0

I am adding String returned by Callback method from Volley's onRespond method.

I have already initialized GetterAndSetter Method inside OnCreate Method.

this is my code:

onRespond(){
       ...
       AddtoList(CreateURL, new VolleyCallback() {
                @Override
                public void onSuccess(String result) {
                    getterAndSetter.addString(result);
                }
            });
       ....
}

My GetterAndSetter Class:

public class GetterAndSetter {
    ArrayList<String> strings = new ArrayList<>();

    public void addString(String string) {
        this.strings.add(string);
    }

    public ArrayList<String> getList(){
        return this.strings;
    }
}

I tries to get all the strings added inside of this getterandsetter's ArrayList via following code inside my another method :

void LoadImages(MainActivity mainActivity){
       ...
        List<String> details = getterAndSetter.getList();
        Log.d("gs", getterAndSetter.getList().toString());
       ...
}

As seen above, I tried to print log, but it print "[]"(Empty String). I have seen alot of Answers on Stackoverflow, but can't solve my problem.

Update : I am adding more code so that you guys can understand the problem.

My OnCreate Method :

 @Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    getterAndSetter = new GetterAndSetter();
    LoadImages(this);
}

LoadImages :

private void LoadImages(MainActivity mainActivity) {
            StringRequest stringRequest = new StringRequest(URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    mView.dismiss();

                    Log.d("Respone", "onResponse: " + response);

                    //  Used to Get List of Images URLs
                    getResponse = ParseJSON(response);

                    List<String> urlList = getResponse.get(0);
                    List<String> titles = getResponse.get(1);
                    List<String> details = getterAndSetter.getList();

                    Log.d("gs", getterAndSetter.getList().toString());

                    for (String urls : urlList) {
                        Log.d("urls", urls);
                    }

                    for (String title : titles) {
                        Log.d("tts", title);
                    }

                    for (String dt : details) {
                        Log.d("dts", dt);
                    }


                }
            }, error -> {
                Log.d(TAG, "onErrorResponse: Error Occured...");
            });
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);
    }

ParseJSON method :

ArrayList<ArrayList<String>> ParseJSON(String URL) {
        try {
            JSONObject root = new JSONObject(URL);
            JSONObject photos = root.getJSONObject("photos");
            JSONArray photo = photos.getJSONArray("photo");

            ArrayList<String> listURLS = new ArrayList<>();
            ArrayList<String> Titles = new ArrayList<>();

            ArrayList<ArrayList<String>> result = new ArrayList<>();

            for (int i = 0; i < photo.length(); i++) {
                JSONObject photosJSONObject = photo.getJSONObject(i);
                String FarmID = photosJSONObject.getString("farm");
                String ServerID = photosJSONObject.getString("server");
                String ID = photosJSONObject.getString("id");
                String SecretID = photosJSONObject.getString("secret");
                String ImageTitle = photosJSONObject.getString("title");

                listURLS.add(i, CreatePhotoURL(FarmID, ServerID, ID, SecretID));
                Titles.add(i, ImageTitle);

                String CreateURL = "https://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=" + API_Key + "&photo_id=" + ID + "&format=json&nojsoncallback=1";

                AddtoList(CreateURL, new VolleyCallback() {
                    @Override
                    public void onSuccess(String result) {
                        getterAndSetter.addString(result);
                    }
                });

                result.add(listURLS);
                result.add(Titles);
            }
            return result;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

AddtoList Method :

public void AddtoList(String CreateURL, VolleyCallback volleyCallback) {
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        StringRequest stringRequest = new StringRequest(CreateURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                JSONObject root;
                try {
                    root = new JSONObject(response);
                    JSONObject photo = root.getJSONObject("photo");

                    String username = photo.getJSONObject("owner").getString("username");
                    String DateTaken = photo.getJSONObject("dates").getString("taken");
                    String Views = photo.getString("views");

                    String str = "Date Taken : " + DateTaken + "\n" + "Views : " + Views + "\n" + "User Name : " + username + "\n";

                    volleyCallback.onSuccess(str);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG, "onErrorResponse: " + error.toString());
            }
        });
        requestQueue.add(stringRequest);
    }

My CallBack

public interface VolleyCallback {
        void onSuccess(String result);
    }
Avi Patel
  • 475
  • 6
  • 23

1 Answers1

0

The problem is that you're trying to print the list BEFORE the callback is being invoked by Volley.

You parse a Json and for each element, you make a new request. After parsing the Json you print the list, it's empty because of the calls you make aren't ended at the moment you print the list.

You need to wait until all the request you start from inside the loop have ended.

Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32
  • I have a doubt. I am getting Strings via printing logs, inside my getter and setter. If your suggestion is valid then isn't logs inside getterAndSetter empty as well? – Avi Patel Jun 19 '18 at 14:16
  • No, if you log the values inside the `addString` method, them shouldn't be empty – Luca Nicoletti Jun 19 '18 at 15:00
  • How can i access them, so that i dont get it Empty? I have also tried making arraylist of URLs and then pass it one by one, which is the same thing. – Avi Patel Jun 19 '18 at 15:12