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.