Question: Why is Google returning an INVALID_REQUEST response? I’m guessing it something to do with the request not being constructed correctly. (Content length is ‘2’)
public void addNewPlace(String latitude, String longitude, String name, String type)
{
int accuracy = 50;
HttpRequest request;
try {
String placeJSON =
"{"+
"\"location\": {" +
"\"lat\": " + latitude + "," +
"\"lng\": " + longitude +
"}," +
"\"accuracy\":" + accuracy + "," +
"\"name\": \"" + name + "\"," +
"\"types\": [\"" + type + "\"]," +
"\"language\": \"en-EU\" " +
"}";
InputStreamContent content = new InputStreamContent("application/json", new ByteArrayInputStream(placeJSON.getBytes("UTF-8")));
HttpHeaders headers = new HttpHeaders();
headers.setContentType("application/json");
JsonHttpContent contentJSON = new JsonHttpContent(new JacksonFactory(), content);
request = t.buildPostRequest(new GenericUrl(PLACES_ADD_URL), contentJSON);
request.setHeaders(headers);
request.getUrl().put("key", "....");
request.getUrl().put("sensor", "true");
System.out.println("JSON: " + placeJSON);
System.out.println("URL: " + request.getUrl());
System.out.println("HEADERS: " + request.getHeaders());
System.out.println("CONTENT LENGTH:" + request.getContent().getLength());
System.out.println("POST:" + request.execute().parseAsString());
}
catch (IOException e)
{
e.printStackTrace();
}
}
Results from the above print statements
JSON: {"location": {"lat": 52.4884149,"lng": -1.8925126},"accuracy":50,"name": "test","types": ["restaurant"],"language": "en-EU" }
URL: https://maps.googleapis.com/maps/api/place/add/json?key=........&sensor=true
HEADERS: {Accept-Encoding=gzip, Content-Type=application/json}
CONTENT LENGTH:2
POST:{
"status" : "INVALID_REQUEST"
}
I have run the JSON through http://jsonlint.com/ and it’s valid. I have also checked it thoroughly against the Google Places API example JSON.
Using
ByteArrayContent.fromString(null, placeJSON)and constructing the JSON string manually works.