我需要在 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/