2017-06-14 267 views
0

我想通过将http POST方法应用于Google Vision API来发送json对象。我正在使用以下代码:向Google Vision API发送请求

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

什么是你的错误的详细信息的64位编码的字符串?响应中是否有扩展的错误信息?你确定你已经正确认证了吗?哦,你的JSON格式不正确,尽管这可能是一个剪切和粘贴错误。请发布*精确* JSON。不应该''功能'是一个数组? –

回答

0

the documentation寻找,features应该是这样的数组:

{ 
    "requests": [ 
    { 
     "image": { 
     "content": "..." 
     }, 
     "features": [ 
     { 
      "type": "WEB_DETECTION", 
      "maxResults": 10 
     } 
     ] 
    } 
    ] 
} 

参见this page

0

我建设我的JSON对象如下:此处可变编码包含图像

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);