1
問題:爲什麼Google返回INVALID_REQUEST響應?我猜這是與請求沒有正確構建有關。從Google Places在添加地點時POST「INVALID_REQUEST」響應
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"
}
我已經通過http://jsonlint.com/運行JSON上述打印語句(內容長度是 '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();
}
}
結果和它的有效。我也對Google Places API示例JSON進行了徹底檢查。