How can I get through this HTTP GET 404 error in java?

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?

+4
source share
1 answer

Creating a URL manually in this way can result in your code not hiding the API key correctly, and you don’t need to add 433 if the host is HTTPS on an HTTP client, this is the code that works:

 import java.net.URI; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIUtils; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; public class GooglePlacesRequest { public static void main(String[] args) throws Exception { // Web API related String apiKey = "YOUR_API_KEY_HERE"; // search params String location = "51.527277,-0.128625";// lat,lon String types = "food"; String name = "pret"; List<NameValuePair> parameters = new ArrayList<NameValuePair>(); parameters.add(new BasicNameValuePair("location", location)); parameters.add(new BasicNameValuePair("radius", "500")); parameters.add(new BasicNameValuePair("types", types)); parameters.add(new BasicNameValuePair("name", name)); parameters.add(new BasicNameValuePair("sensor", "false")); parameters.add(new BasicNameValuePair("key", apiKey)); URL url = new URL( "https://maps.googleapis.com/maps/api/place/search/json"); URI finalURI = URIUtils.createURI( url.getProtocol(), url.getHost(), url.getPort(), url.getPath(), URLEncodedUtils.format(parameters, "UTF-8"), null); HttpGet get = new HttpGet(finalURI); 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()); System.out.println( "Response content is:" ); System.out.println( EntityUtils.toString( response.getEntity() ) ); } catch (Exception e) { System.err.println("HttpClient: An error occurred- "); e.printStackTrace(); } } } 
+3
source

All Articles