【问题标题】:Microsoft Computer Vision API with android volley response code 400带有 android volley 响应代码 400 的 Microsoft 计算机视觉 API
【发布时间】: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


【解决方案1】:

请注意,我们在旧设备上遇到了奇怪的问题。

是由于上传的图片太大造成的。 Api 只返回 400,没有任何解释。

ImageHelper 类具有调整图像大小的代码

【讨论】:

    【解决方案2】:

    您必须将StringRequest 替换为JsonObjectRequest 并去掉Content-type 标头。

    这对我有用:

            final JSONObject jsonBody = new JSONObject();
            jsonBody.put("url", "https://pbs.twimg.com/profile_images/808958766628605952/yB14UlXl_400x400.jpg");
    
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, URL, jsonBody,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            System.out.println(response.toString());
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            String readableError = "";
                            try {
                                readableError = new String(error.networkResponse.data, "utf-8");
                                System.out.println(readableError);
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                        }
                    }) {
                        @Override
                        public Map<String, String> getHeaders() throws AuthFailureError {
                            HashMap<String, String> headers = new HashMap<>();
                            headers.put("Ocp-Apim-Subscription-Key", "XXXXXXXX");
                            return headers;
                        }
                    };
    

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 2015-01-03
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      • 2015-01-03
      • 1970-01-01
      • 2018-01-02
      相关资源
      最近更新 更多