我正在创建一个Android应用程序,用于通过搜索使用谷歌地方api添加和删除谷歌地图附近的地点。我能够从我的应用程序添加新的地方,显示在附近的搜索结果。现在我想删除任何这个地方使用地方删除操作,它总是提供 INVALID_REQUEST 响应。
以下是我删除地点的网址:
String url="https://maps.googleapis.com/maps/api/place/delete/json?key=MY BROWSER KEY";
new delete().execute(url);
我删除地方的代码是:
public class delete extends AsyncTask<String, Void, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(UserBuisness.this);
pDialog.setMessage("deleting..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... url) {
// TODO Auto-generated method stub
StringBuilder placesRemover = new StringBuilder();
for (String placeDeleteURL : url) {
HttpClient httpclientdelete = new DefaultHttpClient();
HttpPost httpPostDelete = new HttpPost(placeDeleteURL);
InputStream inputStream = null;
try {
JSONObject json=new JSONObject();
try {
//place_id contains place_id of place to be deleted
json.put("placeid",place_id);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String postBodyDelete=json.toString();
StringEntity se = new StringEntity(postBodyDelete,HTTP.UTF_8);
httpPostDelete.setEntity(se);
HttpResponse httpResponse = httpclientdelete.execute(httpPostDelete);
inputStream = httpResponse.getEntity().getContent();
if(inputStream != null){
InputStreamReader placesInputDelete = new InputStreamReader(inputStream);
//use buffered reader to process
BufferedReader placesRemoverReader = new BufferedReader(placesInputDelete);
//read a line at a time, append to string builder
String lineIn;
while ((lineIn = placesRemoverReader.readLine()) != null) {
placesRemover.append(lineIn);
}
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return placesRemover.toString();
}
protected void onPostExecute(String result) {
JSONObject response=null;
String sam="";
try {
response=new JSONObject(result);
sam=response.getString("status");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pDialog.dismiss();
Toast.makeText(getApplicationContext(), sam, 1000).show();
}
}
我没有得到 INVALID_REQUEST 的原因,因为网址和帖子正文与google地方操作所需的格式相同,用于删除地点。感谢任何种类的帮助。
答案 0 :(得分:0)
我遇到了同样的问题,并在Google的文档中发现错误。 deleting a place的文档清楚地说明(在两个地方)地方ID的JSON密钥是&#34; placeid&#34;。
POST https://maps.googleapis.com/maps/api/place/delete/json?key=AddYourOwnKeyHere HTTP/1.1
Host: maps.googleapis.com
{
"placeid": "place ID"
}
...
placeid: A string identifying the place that must be deleted, returned from a place search.
正确的密钥实际上是&#34; place_id &#34;,这与其他API调用一致。
因此,您需要更新此行:
json.put("placeid",place_id);
是这样的:
json.put("place_id",place_id);
我知道这是一个迟到的答案,但我希望这仍然有帮助!我还开通了Google企业支持的门票,以确保文档中的此错误得到解决。