【发布时间】:2018-08-12 11:22:50
【问题描述】:
在使用 Android Volley 请求时,我无法从 Microsoft 自定义视觉 API(光学字符识别 API)获取 JSON 响应。
我已经将这种方法与其他 API 一起使用,没有任何问题,但是对于这个 API,我无法让它工作。
String URL = "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/ocr";
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Getting License plate...");
pDialog.setCancelable(false);
pDialog.show();
try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
JSONObject jsonBody = new JSONObject();
jsonBody.put("url", "https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Atomist_quote_from_Democritus.png/338px-Atomist_quote_from_Democritus.png");
final String requestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
pDialog.hide();
Log.i("VOLLEY", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
}) {
@Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Ocp-Apim-Subscription-Key", "123124123123123123213");
return headers;
}
};
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
我收到了回复:
E/Volley: [2163] BasicNetwork.performRequest: Unexpected response code 400 for https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/ocr
使用邮递员时,我没有收到任何错误。
所以希望你能看到我做错了什么。 如果您想让我详细说明任何事情,请告诉我。
谢谢!
【问题讨论】:
-
如果您使用
JsonObjectRequest而不是StringRequest,会发生什么情况?
标签: android api android-volley microsoft-cognitive http-status-code-400