我得到在ResponseFinal log.But循環的響應顯示裏面的物品在的List<String> resultList
響應爲空resultList的Android - ArrayList中返回null之外循環
我不明白爲什麼resultlist正在恢復空loop.Please幫助
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: []
那麼你的JSON響應是什麼樣子?什麼'Log.e(「ResponseFinal」,resultList.toString());'實際打印到logcat? –
返回'null'或空列表? –
發佈您的日誌。 – Pankaj