带标题和正文的HTTPPost方法

时间:2017-10-03 08:55:03

标签: android http-post cloudsight

我需要在Cloudsight上制作一个HTTPPost reguest用于图像识别。我有:

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

Headers: Content-Type  application/json
        Authorization  Cloudsight API_KEY

它说:使用多部分格式编码或base64编码数据参数在端点/图像上使用HTTP POST请求发送图像。 网址: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;
        }

Buth似乎成功地成功了。有人可以提供建议

1 个答案:

答案 0 :(得分:2)

我在看这篇文章时注意到了一些事情。在顶部的第二个片段中,Authorization标头显示为:

Cloudsight API_KEY代替CloudSight API_KEY

不确定这只是一个简短的拼写错误,还是代码中的错误。在代码示例中,有一行读数:

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

应该是:

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

试一试,让我们知道它是怎么回事!