2017-06-14 17 views
0

Google Vision APIに対してhttp POSTメソッドを使用して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オブジェクト(リクエスト)の形式は次のとおりです。the documentationを見て

{"imageContext":"", 
"requests":" 
    {"image": 
     {"content":"..."}, 
    "features": 
     {"type":"WEB DETECTION"} 
     {"maxResults":10} 
    } 
} 
+0

の64ビットエンコードされた文字列が含まれているあなたが得るエラーの詳細は何ですか?応答に拡張エラー情報がありますか?正しく認証されていますか?ああ、あなたのJSONは正しく書式設定されていませんが、カットアンドペーストのエラーかもしれません。 * exact * JSONを投稿してください。そして、 'features'は配列であるべきではありませんか? –

答えて

0

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); 
関連する問題