Reputation: 115
Current I am experiencing an issue with my code where, in the last 3 days, I have been unable to successfully complete delete requests using the Google Places API, documented here.
Up until Sunday this code would execute and run without an issue as long as the requested place met the conditions in the API, and the only responses I received were of the OK or REQUEST_DENIED form.
Now, however, whenever I send a request the only responses I receive are of the form INVALID_REQUEST, which is very inconvenient to say the least. From my understanding, and the testing I performed on this code beforehand, I am complying with the format that they are requesting, so I can't understand why this isn't working.
Can anyone else look at this code and tell me if there are any problems compared to the linked 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;
}
Upvotes: 9
Views: 8163
Reputation: 1468
What I was doing wrong was that I was using the 'id' property of Google, but it turns out that there is also a 'place_id', and you have to use the place_id, and NOT the 'id'.
Upvotes: 0
Reputation: 8819
I'm assuming you've replaced API_KEY_HERE with your API key. Try getting a new key in the developer console, and make sure your "allowed IP address" settings are correct.
Upvotes: 0
Reputation: 1336
Personnally, I received INVALID_REQUEST response to my Google API requests because they were not enough spaced between eachother. I had to insert a sleep for 2 seconds between them for them to work.
Upvotes: 20
Reputation: 495
Another scenario, that happened to me, is that the my datatype for where I was storing the Google Reference ID was a string
(limited to 255 chars), changing the column data type to text
fixed everything. The INVALID_REQUEST response is usually from a bad reference call. Google could've also extended the length of the reference IDs and you wouldn't know it wasn't saving correctly to the DB.
Upvotes: 0
Reputation: 4427
This worked for me:
public String delete(String reference) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://maps.googleapis.com/maps/api/place/delete/xml?sensor=false&key=YOUR_KEY_HERE");
try {
StringEntity se = new StringEntity( "<PlaceDeleteRequest>\n<reference>" + reference + "</reference>\n</PlaceDeleteRequest>", HTTP.UTF_8);
se.setContentType("text/xml");
httppost.setEntity(se);
HttpResponse httpresponse = httpclient.execute(httppost);
HttpEntity resEntity = httpresponse.getEntity();
return(EntityUtils.toString(resEntity));
} catch (IOException e) {
return e.toString();
}
Upvotes: 0
Reputation: 4427
Try refreshing your reference as recommended by the documentation: https://developers.google.com/maps/documentation/places/
It is recommended that stored references for Places be regularly updated.
Upvotes: 0