I am using Apache HTTPClient 4.2 and trying to make the Google Places API , but with problems.
Here is a basic snippet illustrating the problem:
//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(); }
This output I get is a bit like this (with the exception of API_KEY, of course):
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
All this is a little puzzling because:
- I don't have much experience with HTTP REST calls
If I try the getRequestLine () URL in a Simple REST Client Client , I get a status of 200 with data similar to this
{"html_attributions": [], "Results": [], "status": "REQUEST_DENIED"}
but if I use the getURI () version, it works fine.
I'm not sure the problem is that "HTTP / 1.1" is being added or something else. Is this the right way to make a Google Places API request in Java?
source share