google place action delete places gives invalid request

与世无争的帅哥 提交于 2019-12-13 00:06:53

问题


I am creating an android application for adding and deleting places on google map near by search using google places api.I am able to add new places from my application which is displayed in nearby search result.Now when i am trying to delete any of this place using place delete action,it always gives INVALID_REQUEST response.

Here is my url for deleting places:

String url="https://maps.googleapis.com/maps/api/place/delete/json?key=MY BROWSER KEY";
new delete().execute(url);

And my code for deleting place is:

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();


    }



}

I am not getting the reason for INVALID_REQUEST since the url,and post body is of the same format as required by google place action for deleting places.Any kind of help is appreciated.


回答1:


I just ran into this same problem and figured out it's an error in Google's documentation. The documentation for deleting a place clearly says (in two places) that the JSON key for the place id is "placeid".

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.

The correct key though is actually "place_id", which is consistent with the other API calls.

Therefore, in your case you just need to update this line:

json.put("placeid",place_id);

to be this:

json.put("place_id",place_id);

I know this is a late answer, but I hope this still helps! I also opened a ticket with Google enterprise support to ensure this error in the documentation is resolved.



来源:https://stackoverflow.com/questions/26780315/google-place-action-delete-places-gives-invalid-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!