hana hana - 3 days ago 5
Android Question

JsonObjectRequest returns nothing

I want to use Google Places API, to fetch and display the list of restaurants in a certain area.
I tried this but it displays nothing, and I have no errors:

String url="https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-45.730425,4.839938&radius=500&types=food&key=AIzaSyD8vqYPQzJRJa3rOLSC1wqIaUxSN0WcvGc";

JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
Log.d("json", response.toString());
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Log.d("json", "not working");
}
});

Answer

If you try that URL in a web browser it will tell you what the problem is:

{
    "error_message": "This API project is not authorized to use this API. Please ensure this API is activated in the Google Developers Console: https://console.developers.google.com/apis/api/places_backend?project=_",
    "html_attributions": [],
    "results": [],
    "status": "REQUEST_DENIED"
}

So it looks like you haven't enabled the API in the developer console.

Go to https://console.developers.google.com/

And then click on the project you want, in the left hand menu click API manager, then library, search at the top for Places and then click on the result you want, from here you should see a button that says "ENABLE"

Click on that then try your request again and it should work.

EDIT:

A link is provided in the response for you to use:

https://console.developers.google.com/apis/api/places_backend

Click on that and there should be the option to enable the API on this page

Comments