Apache HTTPClient 4.2を使用しており、 Google Places API クエリを作成しようとしていますが、問題があります。
この問題を説明するための基本的なスニペットを次に示します。
//Web API related
String apiKey = "API_KEY";
//search params
String location = "51.527277,-0.128625";//lat,lon
int rad = 500;
String types = "food";
String name = "pret";
String getURL = "/maps/api/place/search/json?location="+location+"&radius="+rad+"&types="+types+"&name="+name+"&sensor=false&key="+apiKey;
HttpHost host = new HttpHost("maps.googleapis.com",443,"https");
HttpGet get = new HttpGet(host.toURI() + getURL);
System.out.println("using getRequestLine(): " + get.getRequestLine());
System.out.println("using getURI(): " + get.getURI().toString());
DefaultHttpClient httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
try {
HttpResponse response = httpClient.execute(get);
System.out.println("response: " + response.getStatusLine().toString());
} catch (Exception e) {
System.err.println("HttpClient: An error occurred- ");
e.printStackTrace();
}
私が得ているこの出力は、次のように見えます (もちろん API_KEY を除いて):
using getRequestLine(): GET https://maps.googleapis.com:443/maps/api/place/search/json?location=51.527277,-0.128625&radius=500&types=food&name=pret&sensor=false&key=API_KEY HTTP/1.1
using getURI(): https://maps.googleapis.com:443/maps/api/place/search/json?location=51.527277,-0.128625&radius=500&types=food&name=pret&sensor=false&key=API_KEY
response: HTTP/1.1 404 Not Found
次の理由から、それはすべて少し不可解です。
- HTTP REST 呼び出しの経験があまりない
Simple REST Client Chrome 拡張機能で getRequestLine() url を試すと、次のようなデータでステータス 200 が返されます。
{ "html_attributions" : []、"results" : []、"status" : "REQUEST_DENIED" }
しかし、getURI() バージョンを使用すると、正常に動作します。
問題が追加される「HTTP/1.1」なのか、それとも何か他のものなのかはわかりません。これは Java から Google Places API クエリを作成する適切な方法ですか?