Android的JSONArray到ArrayList中 [英] Android JSONArray to ArrayList
本文介绍了Android的JSONArray到ArrayList中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图解析JSONArray成和ArrayList在我的Android应用程序。 PHP脚本正确retuns预期的结果,但是Java的失败,一个空指针异常的 resultsList.add(图)
公共无效agencySearch(字符串TSEARCH){
//设置URL由镇搜索
字符串url_search_agency =HTTP://www.infinity$c$cservices.com/get_agency_by_city.php;
//建筑参数为搜索
名单<的NameValuePair> PARAMS =新的ArrayList<的NameValuePair>();
params.add(新BasicNameValuePair(城,TSEARCH));
//从URL获取JSON字符串
JSONArray JSON = jParser.getJSONFromUrl(url_search_agency,PARAMS);
的for(int i = 0; I< json.length();我++){
HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
尝试 {
JSONObject的C =(的JSONObject)json.get(我);
//填充地图
迭代器ITER = c.keys();
而(iter.hasNext()){
字符串currentKey =(字符串)iter.next();
map.put(currentKey,c.getString(currentKey));
}
resultsList.add(图)
}
赶上(JSONException E){
e.printStackTrace();
}
};
MainActivity.setResultsList(resultsList);
}
解决方案
试试这样可以帮助你,
公共无效agencySearch(字符串TSEARCH){
//设置URL由镇搜索
字符串url_search_agency =HTTP://www.infinity$c$cservices.com/get_agency_by_city.php;
//建筑参数为搜索
名单<的NameValuePair> PARAMS =新的ArrayList<的NameValuePair>();
params.add(新BasicNameValuePair(城,TSEARCH));
//从URL获取JSON字符串
JSONArray JSON = jParser.getJSONFromUrl(url_search_agency,PARAMS);
ArrayList的< HashMap的<字符串,字符串>> resultsList =新的ArrayList< HashMap的<字符串,字符串>>();
的for(int i = 0; I< json.length();我++){
HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
尝试 {
JSONObject的C = json.getJSONObject(位置);
//填充地图
迭代器<字符串> ITER = c.keys();
而(iter.hasNext()){
串currentKey = it.next();
map.put(currentKey,c.getString(currentKey));
}
resultsList.add(图)
}
赶上(JSONException E){
e.printStackTrace();
}
};
MainActivity.setResultsList(resultsList);
}
I am trying to parse a JSONArray into and ArrayList in my android app. The PHP script correctly retuns the expected results, however the Java fails with a null pointer exception at resultsList.add(map)
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);
}
解决方案
try like this may help you,
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);
ArrayList<HashMap<String, String>> resultsList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < json.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
try {
JSONObject c = json.getJSONObject(position);
//Fill map
Iterator<String> iter = c.keys();
while(iter.hasNext()) {
String currentKey = it.next();
map.put(currentKey, c.getString(currentKey));
}
resultsList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
};
MainActivity.setResultsList(resultsList);
}
这篇关于Android的JSONArray到ArrayList中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文