1

我需要在 Cloudsight 上进行 HTTPPost 请求以进行图像识别。我有:

BASE URL https://api.cloudsight.ai/v1/images

Headers: Content-Type  application/json
        Authorization  Cloudsight API_KEY

它说:通过在端点 /images 上使用 HTTP POST 请求发送图像,并带有 multipart-form-encoded 或 base64 编码的数据参数。网址:http ://docs.cloudsight.apiary.io/#reference/0/images-collection/send-an-image-for-identification?console=1 到目前为止,我已经做到了:

private class HTTPPOSTReguest extends AsyncTask<String, Void, String> {
        ProgressDialog dialog;
        String result = "";
        @Override

        protected void onPreExecute() {

        }

        @Override
        protected String doInBackground(String... params) {

            try {

                HttpResponse response = null;
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(BASE_URL);
                httppost.addHeader("Content-Type", "application/json");
                httppost.addHeader("Authorization", "CloudSight buEA_pC6K7FXT60inM2eUQ");

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("images", encodedImage));
               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                response = httpclient.execute(httppost);
                int responseCode = response.getStatusLine().getStatusCode();

                
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    String responseBody = EntityUtils.toString(entity);
                    result = responseBody;
                }
            }  catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }

布斯似乎可以做出成功的请求。有人可以提供建议吗

4

1 回答 1

2

我在浏览此内容时注意到了几件事。在顶部的第二个片段中,授权标头显示为:

Cloudsight API_KEY代替CloudSight API_KEY

不知道这只是一个快速的错字还是在代码中。在代码示例中,有一行内容:

HttpPost httppost = new HttpPost("https://api.cloudsightapi.ai");

那应该是:

HttpPost httppost = new HttpPost("https://api.cloudsight.ai/v1/images");

试一试,让我们知道进展如何!

于 2017-10-03T21:41:16.543 回答