Android JSONArray to ArrayList

前端 未结 2 1219
再見小時候
再見小時候 2021-01-16 01:57

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 excep

2条回答
  •  青春惊慌失措
    2021-01-16 02:20

    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 params = new ArrayList();
            params.add(new BasicNameValuePair("City", tsearch));
    
            // Getting JSON string from URL
            JSONArray json = jParser.getJSONFromUrl(url_search_agency, params);
    
           ArrayList> resultsList = new  ArrayList>();
    
            for (int i = 0; i < json.length(); i++) {
                HashMap map = new HashMap();
    
                try {
                    JSONObject c = json.getJSONObject(position);
                    //Fill map
                   Iterator 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);
    
        }
    

提交回复
热议问题