java - E/Error JSONException: No value for categories in android -
i have json data , tried parse android. faced jsonexception;- no value categories. me
my mainactivity here:-
public class mainactivity extends appcompatactivity implements fragmentdrawer.fragmentdrawerlistener { //private static string tag = mainactivity.class.getsimplename(); private toolbar mtoolbar; private fragmentdrawer drawerfragment; string data1,data2,data3,data4,data5; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mtoolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(mtoolbar); getsupportactionbar().setdisplayshowhomeenabled(true); drawerfragment = (fragmentdrawer) getsupportfragmentmanager().findfragmentbyid(r.id.fragment_navigation_drawer); drawerfragment.setup(r.id.fragment_navigation_drawer, (drawerlayout) findviewbyid(r.id.drawer_layout), mtoolbar); drawerfragment.setdrawerlistener(this); // display first navigation drawer view on app launch displayview(0); log.d("oncreate method",""); new productsasyntask().execute("http://opencart.codeniques.com/myshop/?route=feed/web_api/product&id=30&key=test123"); } public class productsasyntask extends asynctask<string,void,void>{ progressdialog dialog; protected void onpreexecute(){ super.onpreexecute(); log.d("in onpreexceute",""); dialog = new progressdialog(mainactivity.this); dialog.setmessage("loading, please wait"); dialog.settitle("connecting server"); dialog.show(); dialog.setcancelable(false); } protected void doinbackground(string... param){ try{ log.d("in doinbackground",""); httpclient client= new defaulthttpclient(); httppost post = new httppost(param[0]); httpresponse response = client.execute(post); int status = response.getstatusline().getstatuscode(); if(status == 200){ log.d("status",""+status); httpentity entity = response.getentity(); string data = entityutils.tostring(entity); jsonobject jsonobject = new jsonobject(data); jsonarray jarray = jsonobject.getjsonarray("categories"); for(int = 0;i < jarray.length();i++){ jsonobject jsonobject1 = jarray.getjsonobject(i); data1 = jsonobject1.getstring("name"); log.d("hello ",data1); jsonobject jsonobject2 = jsonobject1.getjsonobject("children"); data2 = jsonobject2.getstring("name"); log.d("hello2 ",data2); jsonobject jsonobject3 = jsonobject2.getjsonobject("children_lv3"); data3 = jsonobject3.getstring("name"); log.d("hello3 ",data3); data4 = jsonobject3.getstring("href"); log.d("hello4 ",data4); data5=jsonobject2.getstring("href"); log.d("hello5 ",data5); } } }catch(ioexception e){ log.e("error ioexception :",e.getmessage()); }catch (jsonexception e){ log.e("error jsonexception",e.getmessage()); } return null; } protected void onpostexecute(void avoid) { super.onpostexecute(avoid); dialog.dismiss(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } if(id == r.id.action_search){ toast.maketext(getapplicationcontext(), "search action selected!", toast.length_short).show(); return true; } return super.onoptionsitemselected(item); } @override public void ondraweritemselected(view view, int position) { displayview(position); } private void displayview(int position) { fragment fragment = null; string title = getstring(r.string.app_name); switch (position) { case 0: fragment = new homefragment(); title = getstring(r.string.title_home); break; case 1: fragment = new friendsfragment(); title = getstring(r.string.title_friends); break; case 2: fragment = new messagesfragment(); title = getstring(r.string.title_messages); break; default: break; } if (fragment != null) { fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.replace(r.id.container_body, fragment); fragmenttransaction.commit(); // set toolbar title getsupportactionbar().settitle(title); } }
}
i new in android developing please me. in advance!
its because json
data doesnt contain categories
jsonarray
so check first this:-
if(jsonobject.has("categories")) { jsonarray jarray = jsonobject.getjsonarray("categories"); for(int = 0;i < jarray.length();i++){ jsonobject jsonobject1 = jarray.getjsonobject(i); data1 = jsonobject1.getstring("name"); log.d("hello ",data1); jsonobject jsonobject2 = jsonobject1.getjsonobject("children"); data2 = jsonobject2.getstring("name"); log.d("hello2 ",data2); jsonobject jsonobject3 = jsonobject2.getjsonobject("children_lv3"); data3 = jsonobject3.getstring("name"); log.d("hello3 ",data3); data4 = jsonobject3.getstring("href"); log.d("hello4 ",data4); data5=jsonobject2.getstring("href"); log.d("hello5 ",data5); } }
try checking json
data first, when see jsonexception;- no value for
Comments
Post a Comment