我想解析一個JSONArray到和我的Android應用程序中的ArrayList。 PHP腳本正確retuns預期的結果,但是Java的失敗,一個空指針異常的resultsList.add(map)
Android的JSONArray ArrayList
public void agencySearch(String tsearch) {
// Setting the URL for the Search by Town
String url_search_agency = "http://www.infinitycodeservices.com/get_agency_by_city.php";
// Building parameters for the search
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("City", tsearch));
// Getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url_search_agency, params);
for (int i = 0; i < json.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
try {
JSONObject c = (JSONObject) json.get(i);
//Fill map
Iterator iter = c.keys();
while(iter.hasNext()) {
String currentKey = (String) iter.next();
map.put(currentKey, c.getString(currentKey));
}
resultsList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
};
MainActivity.setResultsList(resultsList);
}
,你初始化'resultsList'在你的代碼? – 2014-11-08 07:07:59
這是因爲'resultsList'爲空。 – immibis 2014-11-08 07:19:57
你有初始化resultList? – micky 2014-11-08 07:20:26