-1

I'm new to this android programming and I'm having a problem with JSON parsing. Here is the JSON URL that I want to parse Billers and view to a ListView. I have some code that I've used and successfully extracted the values that I want. The problem is that I can't or to be precise I'm having a problem on parsing JSON array. Here is the code that I use for Parsing JSON array:

public class BillersJsonParser {
String mUrl;

public BillersJsonParser(String url){
    this.mUrl = url;
}

public BillersModel getCategories(){
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(mUrl);

    try {
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();

        String result = sb.toString();
        return parse(result);
    } catch (JSONException e) {
        return null;
    } catch (ClientProtocolException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
}

private BillersModel parse(String result) throws JSONException{
    BillersModel billerList = new BillersModel();
    
    JSONObject jObject = new JSONObject(result);
    
    JSONArray bList = jObject.getJSONArray("billers");
    
    Log.e("BJSONParser 2nd Lvl: ", bList.toString());
    int length = bList.length();
    for (int i = 0; i < length; i++){
        billerList.setID(bList.getJSONObject(i).getString("bid"));
        billerList.setName(bList.getJSONObject(i).getString("name"));
        billerList.setStatus(bList.getJSONObject(i).getString("status"));
        billerList.setDateAdded(bList.getJSONObject(i).getString("date_added"));
    }
    return billerList;
}

}

Every time I run this code I got a Null exception error and I can't figure it out. I suspect that the error start when eclipse try to run this part of the code JSONArray bList = jObject.getJSONArray("billers");. Any help will be greatly appreciated.

Community
  • 1
  • 1
cha1988
  • 39
  • 8
  • possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – Selvin May 24 '13 at 13:00
  • 1
    json is utf-8, not iso – njzk2 May 24 '13 at 13:04
  • If you are using Eclipse, you should have a Stacktrace where you can even click to go to the line that throwed the Exception. At least it would be helpful to se the StackTrace and line numbering... – Martin May 24 '13 at 13:04
  • 2
    don't discard your exceptions. e.printstacktrace them – njzk2 May 24 '13 at 13:05
  • 2
    `catch (JSONException e) { return null; }` code like this is extremely bad practice – David May 24 '13 at 13:06

1 Answers1

1

According to Billers your jObject IS JSONArray. And what you are doing - is assuming that jObject is JSONObject that has array property named billers like

{"billers":[{"bid":"1","name":"biller1","logo":"","status":"1","date_added":"2013-03-20 23:03:55"},{"bid":"2","name":"biller2","logo":"","status":"1","date_added":"2013-03-20 23:03:55"},{"bid":"3","name":"biller3","logo":"","status":"1","date_added":"2013-03-20 23:04:06"},{"bid":"4","name":"biller4","logo":"","status":"1","date_added":"2013-03-20 23:04:06"}]}

And yes, @njzk2 is right, json is UTF-8. Also, your reading with adding line break "\n" looks strange.

ooops
  • 336
  • 2
  • 14
  • I can't seem to get it. I use the code to parse Json value like this {"response":"false","errors":""} and it worked just fine. I'm confused on should I use that for loop to display my data on a listview. – cha1988 May 24 '13 at 14:03
  • By the way that JSONArray("billers"); I think thats is the problem. billers is not json array node. I just did some assumption on that because JSONArray(" – cha1988 May 24 '13 at 14:07
  • if http://888partners.com/api/billers is actual, the responce contains only json array, but not an object that have billers property. So jObject.getJSONArray("billers") is incorrect. Try JSONArray jObject = new JSONArray(result) instead and iterate through it's values. Anyway, it is sometimes helpful to put breakpoint before crashing code and execute code step-by-step. – ooops May 24 '13 at 14:15
  • And don't forget to change iso to utf-8. And probably you'll find it useful to add exception logging/printing in catch block. That will help you to find out in which line crash occured. – ooops May 24 '13 at 14:21
  • 1
    Okay I'll try that one for now and try to figure out something myself before asking you guys again for help. Thanks alot guys. – cha1988 May 24 '13 at 14:22