Я бы хотел протестировать API облачного видения в своем приложении для Android.
API активирован и работает в Google Cloud Explorer, как показано на рисунке ниже.
![Google APIs Explorer](https://i.stack.imgur.com/ZQDZN.png)
Как сделать простой пост-запрос объектом RequestQueue?
Я много пробовал, но моя программа всегда переходит в метод onErrorResponse
.
мой код:
private static String apiKey = "myAPIKey";
private static final String baseURI = "https://vision.googleapis.com/v1/images:annotate?fields=responses%2FfaceAnnotations&key=" + apiKey;
private static String exampleRequest = "{\"requests\":[{\"image\":{\"source\":{\"imageUri\":\"https://wikipedia.de/img/Wikipedia-logo-v2-de.svg\"}},\"features\":[{\"type\":\"TEXT_DETECTION\"}]}]}";
private static void testRequest(){
JSONObject jsonObject = getJSONRequest(exampleRequest);
Response.Listener<JSONObject> responseListener = getResponseListener();
Response.ErrorListener errorListener = getErrorListener();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(baseURIPost, jsonObject, responseListener, errorListener);
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(jsonObjectRequest);
}
private static Response.ErrorListener getErrorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
};
}
private static Response.Listener<JSONObject> getResponseListener() {
return new Response.Listener() {
@Override
public void onResponse(Object response) {
}
};
}
private static JSONObject getJSONRequest(String json) {
try {
return new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}