【发布时间】:2012-03-20 08:59:53
【问题描述】:
目前我的代码遇到问题,在过去 3 天内,我无法使用 Google Places API 成功完成删除请求,记录在 here。
直到周日,只要请求的地点满足 API 中的条件,此代码将毫无问题地执行和运行,并且我收到的唯一响应是 OK 或 REQUEST_DENIED 形式。
但是,现在,每当我发送请求时,我收到的唯一响应都是 INVALID_REQUEST 形式,至少可以说非常不方便。根据我的理解,以及我事先对此代码执行的测试,我符合他们要求的格式,所以我不明白为什么这不起作用。
其他人可以看看这段代码并告诉我与链接的 API 相比是否有任何问题?
public boolean delete(String reference)
{
try
{
System.out.println(reference);
String url = "https://maps.googleapis.com/maps/api/place/delete/xml?sensor=false&key=API_KEY_HERE";
String data = "<PlaceDeleteRequest>\n<reference>" + reference + "</reference>\n</PlaceDeleteRequest>";
System.out.println(data);
URL xmlUrl = new URL(url);
HttpPost request = new HttpPost(xmlUrl.toURI());
request.setEntity(new StringEntity(data));
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
BufferedReader input = new BufferedReader(new InputStreamReader(entity.getContent()));
String line = "";
while ((line = input.readLine()) != null)
{
System.out.println(line);
if (line.contains("<status>"))
{
String[] s1 = line.split(">");
String[] s2 = s1[1].split("<");
if (s2[0].equalsIgnoreCase("ok"))
{
return true;
}
else
{
return false;
}
}
}
input.close();
}
catch(Exception e)
{
return false;
}
return false;
}
【问题讨论】:
-
我刚刚在 JSON 请求中遇到了这个问题,因为文档中的地点 ID 键应该是“place_id”而不是“placeid”。在这里查看我的答案:stackoverflow.com/a/28287549/4448436
-
placeid 可以与 Google API 文档的示例链接一起正常工作。但它不适用于我获得的 ID。我对两者都使用相同的 API 密钥。