我在ResponseFinal日志中得到List<String> resultList
响应为空,但是在循环内部,响应显示resultList中的项目
我不明白为什么resultlist在循环外返回空值。请帮忙
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;
}
我的日志:
E/Response: [1660309, 1660410]
E/ResponseFinal: []
最佳答案
在oncreate()
之前声明List(在类级别定义resultList
)
List<String> resultList = new ArrayList<String>();