1

I'm getting the List<String> resultList response as empty in ResponseFinal log.But inside the loop the response is showing items in resultList

I don't understand why resultlist is returning empty outside the loop.Please help

public List<String> getParseJson(String sName)
{
    final List<String> resultList = new ArrayList<String>();

    String name=sName.replace(" ", "%20");
    StringRequest stringRequest = new StringRequest(Request.Method.GET, Constants.BASE_URL + "/suggest_item/items/"+name +"/"+ Constants.API_KEY,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                // Result handling
                try {
                    JSONObject jsonObj = new JSONObject(response.toString());
                    JSONArray predsJsonArray = jsonObj.getJSONArray("Item");

                    for (int i = 0; i < predsJsonArray.length(); i++) {
                        System.out.println( predsJsonArray.get(i).toString());
                        resultList.add(predsJsonArray.get(i).toString());
                        Log.e("Response",resultList.toString());
                    }
                } catch (JSONException e) {
                    Log.e("JSOn", "Cannot process JSON results", e);
                }
           }
      }, new Response.ErrorListener() {
           @Override
           public void onErrorResponse(VolleyError error) {
                // Error handling
                System.out.println(error.toString());

           }
     });

     // Adding request to request queue
     Volley.newRequestQueue(context).add(stringRequest);

     Log.e("ResponseFinal",resultList.toString());
     return resultList;
}

My log :

E/Response: [1660309, 1660410]
E/ResponseFinal: []
3
  • what does your json response look like? and what does Log.e("ResponseFinal",resultList.toString()); actually print to logcat? Commented Dec 18, 2015 at 6:57
  • Return null OR empty list ?? Commented Dec 18, 2015 at 7:00
  • Have you got the values from outside the for loop ? Even I'm also having same problem. I'm retrieving data from firebase in onDataChange method. I defined the arraylist at class level only. All values are saved in the arraylist inside the onDataChange method and the log shows perfectly but when I tried to show the log outside the onDataChange method then it shows null pointer exception in logcat. My arraylist is empty. What's the issue behind this ? I tried with string array,stringbuilder too.... @nithin Commented Dec 5, 2016 at 4:11

4 Answers 4

2

declare List before oncreate() (defining the resultList at class level)

List<String> resultList = new ArrayList<String>();
Sign up to request clarification or add additional context in comments.

Comments

2

You are returning list before response is coming that's why you are getting empty list. You can do something like below code which can get you list after response.

Step 1: Make a method name it onSuccess(List<String> list) with your getParseJson() method

public void onSuccess(List<String> list){
  // do your code here after getting the list
}

//make this method as void
public void getParseJson(String sName){
    //your code which you have already implemented
}

Step 2: call the above method from onResponse() method which is declared inside your getParseJson() method as follows:

 @Override
   public void onResponse(String response) {
                // Result handling
                try {
                    JSONObject jsonObj = new JSONObject(response.toString());
                    JSONArray predsJsonArray = jsonObj.getJSONArray("Item");

                    for (int i = 0; i < predsJsonArray.length(); i++) {
                        System.out.println( predsJsonArray.get(i).toString());
                        resultList.add(predsJsonArray.get(i).toString());
                        Log.e("Response",resultList.toString());
                    }

                } catch (JSONException e) {
                    Log.e("JSOn", "Cannot process JSON results", e);
                }

              //call here
              onSuccess(resultList);
}

EDIT: If you are calling getParseJson() from another class then call it as follows:

//If you are having paramaterized constructor then you can give parameter 
 new YourClassName(){
     @Override
     public void success(List<String> list) {
            // do your code here after getting the list
     }
 }.getParseJson(String response);

Comments

1

The list in which you added is a copy and has scope only inside public void onResponse(String response). Hence, the actual list is empty as it was when defined. so, the final log displays empty.

Comments

1

I don't understand why resultList is returning empty outside the loop...

It is empty because your request runs asynchronously. So, you create your request along with response listeners, pass it to the Volley and then getParseJson() returns empty list - because request wasn't performed yet, and the other code will not wait for it to perform. After that, when request is performed (and it's performed asynchronously), your response listener adds the JSON values to the resultList but it's too late.

Rewrite your code so it handles the asynchronous nature of Volley requests.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.