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: []
Log.e("ResponseFinal",resultList.toString());actually print to logcat?nullOR empty list ??