android - 带有 header 和正文的 HTTPPost 方法

标签 android http-post cloudsight

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

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

Headers: Content-Type  application/json
        Authorization  Cloudsight API_KEY

它说:通过在端点/images 上使用 HTTP POST 请求发送图像,并使用多部分形式编码或 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;
        }

但似乎可以成功提出请求。有人可以提供建议

最佳答案

在浏览本文时,我注意到了一些事情。在自上而下的第二个 fragment 中,授权 header 显示为:

Cloudsight API_KEY 而不是 CloudSight API_KEY

不确定这只是一个快速的拼写错误还是代码中的错误。在代码示例中,有一行内容为:

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

那应该是:

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

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

关于android - 带有 header 和正文的 HTTPPost 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46540737/

相关文章:

android - 无法关闭 android 中的警报对话框

android - 在android中使用post在服务器上上传图片是给源文件不存在?

google-apps-script - 使用 Google Apps Script Web 应用绕过 Telegram Bot API

Javascript 授权 HTTP header

ios - 使用 camfind 快速上传图片请求

java - 在互联网连接打开时打开 Android 应用程序,否则显示无互联网连接消息

java - 如何在属于同一容器 Activity 的两个 fragment 之间传递数据?

android - 发布到 facebook 组

rest - 使用 POSTMAN 或任何其他方式批量 POST/PUT API 请求

javascript - 在 httpClient 上发送带有方括号的 post params