我想通过使用http POST方法向Google Vision API发送json对象。我使用以下代码:
URL url = new URL("https://vision.googleapis.com/v1/images:annotate?key=<API-KEY>");
HttpsURLConnection http = (HttpsURLConnection)url.openConnection();
http.setDoOutput(true);
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type", "application/json");
http.connect();
DataOutputStream wr = new DataOutputStream(http.getOutputStream());
wr.writeBytes(request.toString());
Log.v("JSON",request.toString());
wr.flush();
wr.close();
我收到错误请求错误。需要帮助。我的json对象(请求)的格式如下:
{"imageContext":"",
"requests":"
{"image":
{"content":"..."},
"features":
{"type":"WEB DETECTION"}
{"maxResults":10}
}
}
答案 0 :(得分:0)
查看the documentation,features
应该是这样的数组:
{
"requests": [
{
"image": {
"content": "..."
},
"features": [
{
"type": "WEB_DETECTION",
"maxResults": 10
}
]
}
]
}
另见this page。
答案 1 :(得分:0)
我正在构建我的json对象,如下所示:这里变量编码包含图像的64位编码字符串
JSONObject request = new JSONObject();
//image object
JSONObject i = new JSONObject();
i.put("content",encoded);
//feature object
JSONObject features = new JSONObject();
List<JSONObject> featureList = new ArrayList<>();
JSONObject f = new JSONObject();
f.put("type","WEB_DETECTION");
f.put("maxResults",10);
featureList.add(f);
List<JSONObject> requestList = new ArrayList<>();
JSONObject r = new JSONObject();
r.put("image",i);
r.put("features",featureList);
requestList.add(r);
//final json object
request.put("imageContext","");
request.put("requests",requestList);