2017-06-14 172 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);