0

I am trying to make this application in Android, I am getting data from foursquare's API in JSON format and I need to Parse it to present it in another intent. I am using Android's volley library to get the JSON but the problem is the onResponse() function of JsonObjectRequest has no return parameter.so I cannot get the JSON object gotten from url outside of the the onResponse. I haven't worked with volley before and hence don't know much about it, any help is appreciated. Here is the code that I am trying to make it work.

Edit: The main problem I'm facing is that I cannot assign a value to global variable in this case myPlaces inside the JsonObjectRequest's onResponse method. Or to be exact, the variable assigned inside means nothing outside, thus in the last line

            Toast.makeText(MainActivity.this, myPlaces[2].getName(), Toast.LENGTH_LONG).show();

when I try to access the myPlaces[2] it gives me an null pointer exception.

Thanks.

 public void onClick(View v) {

            RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(urlString, null, new com.android.volley.Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    try {
                        JSONObject meta = response.getJSONObject("meta");
                        String status = meta.getString("code");
                        Toast.makeText(MainActivity.this, status, Toast.LENGTH_SHORT).show();


                        if(status.equals("200"))
                        {
                            JSONObject responseJson = response.getJSONObject("response");
                            JSONArray venues = responseJson.getJSONArray("venues");
                            Places[] tempPlaces = new Places[venues.length()];
                            for (int i = 0 ; i < venues.length(); i++)
                            {
                                Places place = new Places();
                                JSONObject venueObject = venues.getJSONObject(i);
                                place.setName(venueObject.getString("name"));
                                JSONObject locatVenue = venueObject.getJSONObject("location");
                                place.setLat(locatVenue.getDouble("lat"));
                                place.setLon(locatVenue.getDouble("lng"));
                                tempPlaces[i] = place;
                            }

                            Toast.makeText(MainActivity.this, tempPlaces[2].getName(), Toast.LENGTH_LONG).show();
                            myPlaces = tempPlaces;
                        }

                        else
                        {
                            Toast.makeText(MainActivity.this, "No response from API", Toast.LENGTH_LONG).show();
                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(MainActivity.this, "There is some error here", Toast.LENGTH_LONG).show();
                    }

                }
            }, new com.android.volley.Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, "There has been some error", Toast.LENGTH_LONG).show();
                }
            });

            requestQueue.add(jsonObjectRequest);
            Toast.makeText(MainActivity.this, myPlaces[2].getName(), Toast.LENGTH_LONG).show();
Astronautilus
  • 83
  • 1
  • 9
  • Possible duplicate of [using inner class object in outer class java](http://stackoverflow.com/questions/18292824/using-inner-class-object-in-outer-class-java) – pczeus Mar 06 '16 at 01:37

1 Answers1

1

Volley itself isn't an inner class; the response is an anonymous class.

You don't need a return in Volley, you just use the variables already defined in your class.

I'm assuming myPlaces is a field in your class? Otherwise, I'm not sure where it is declared outside the onClick..

This line assigns myPlaces and looks like it would work fine

myPlaces = tempPlaces;

You could define a method in your class to parse the whole JSONObject instead of needing to return from Volley. This just passes the logic to another method, so you don't need to think about "returning" inside Volley.

public void parseJSON(JsonObject object) 

And pass the response from volley into that and do your normal parsing and variable assignment and you can Toast myPlaces inside that method.

Also, note that Volley is asynchronous, meaning you aren't guaranteed an immediate result, so

Toast.makeText(MainActivity.this, myPlaces[2].getName(), Toast.LENGTH_LONG).show();

Would likely have thrown either a NullPointerException or IndexOutOfBoundsException because myPlaces was either undeclared or empty before the Volley request. I say that because it does not appear to be assigned before the Volley request.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Oh this was really, helpful. now I feel stupid for not thinking about the creating the method and just passing down the object. Thanks. Hope this helps other people. – Astronautilus Mar 06 '16 at 04:31
  • Your main problem is that volley is asynchronous, so you were getting a NullPointerException – OneCricketeer Mar 06 '16 at 04:55