arinze arinze - 4 months ago 35
JSON Question

Display Json data in android

i am trying to display json data in an android application but i have having difficulty i think is may have to do with the way the json file was formated .i want to get the value of name and code in the headers array. and it is not working

this is the json file

{
status: "SUCCESS",
headers:
{
id: "4",
name: "GLO",
code: "GLO",
background_color_code: "15B709",
text_color_code: "ffffff",
statusMessage: "Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform"
},
statusMessage: "Movies Loaded successfully",
caption: "Discover"
}


this is the javaclass

package com.example.cann;

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONStringer;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;

import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class CategoryActivity extends ListActivity {

private static final String TAG_POSTS = "headers";
public static final String TAG_PIC = "name";
public static final String TAG_NOTE = "code";
JSONParser jParser;
private static final String URL_CATEGORY = "https://dobox.tv/api/home.json?platform=Android&api=1.1";
private BaseAdapter mAdapter;
private ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
lv = getListView();
lv.setDivider(null);

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {

Toast.makeText(CategoryActivity.this, "Item selected: " + position,
Toast.LENGTH_LONG).show();

// Uncomment this to start a new Activity for a chosen item
/* Intent i = new Intent(getApplicationContext(),
ItemListActivity.class);

String category_id = ((TextView) view
.findViewById(R.id.category_id)).getText()
.toString();
i.putExtra("category_id", category_id);

startActivity(i);*/
}
});

new LoadComments().execute();
}

class LoadComments extends AsyncTask<Void, Void, ArrayList<HashMap<String,String>>> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(CategoryActivity.this);

pDialog.setIndeterminate(false);
pDialog.setCancelable(true);

}

@Override
protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {

ArrayList<HashMap<String, String>> categoryList = new ArrayList<HashMap<String, String>>();
jParser = new JSONParser();

JSONObject json = jParser.getJSONFromUrl(URL_CATEGORY);

try {
JSONArray categories =json.getJSONArray("headers");
for (int i = 0; i < categories.length(); i++) {
String state = categories.getJSONObject(i).getString("name");
String status = categories.getJSONObject(i).getString("code");

HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_PIC, state);
map.put(TAG_NOTE, status);
categoryList.add(map);

}
}catch (Throwable e){
e.printStackTrace();
}
return categoryList;
}

@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);
if(pDialog.isShowing()){
pDialog.dismiss();
}
mAdapter = new CategoryListAdapter(CategoryActivity.this,result);
lv.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}
}
}


i am getting this error

07-24 12:20:50.849 16574-16591/com.example.cann W/System.err? org.json.JSONException: Value at headers of type java.lang.String cannot be converted to JSONArray
07-24 12:20:50.852 16574-16591/com.example.cann W/System.err? at org.json.JSON.typeMismatch(JSON.java:100)
07-24 12:20:50.852 16574-16591/com.example.cann W/System.err? at org.json.JSONObject.getJSONArray(JSONObject.java:588)
07-24 12:20:50.853 16574-16591/com.example.cann W/System.err? at com.example.cann.CategoryActivity$LoadComments.doInBackground(CategoryActivity.java:82)
07-24 12:20:50.853 16574-16591/com.example.cann W/System.err? at com.example.cann.CategoryActivity$LoadComments.doInBackground(CategoryActivity.java:61)

Answer

The JSON file you provided is not in a valid JSON format. And also 'headers' attribute contains a JSONObject, not JSONArray. And you got JSON Exception when you tries to get JSONObject as JSONArray.

You may try this:

{
   "status": "SUCCESS",
   "headers": [
   {
      "id": "4",
      "name": "GLO",
      "code": "GLO",
      "background_color_code": "15B709",
      "text_color_code": "ffffff",
      "statusMessage": "Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform"
   },
   {
      "id": "5",
      "name": "PLU",
      "code": "PLU",
      "background_color_code": "15B709",
      "text_color_code": "ffffff",
      "statusMessage": "Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform"
   }
   ],
   "statusMessage": "Movies Loaded successfully",
   "caption": "Discover"
}

Please first check whether your JSON data is valid or not. You may take help from here or here