json - Android JSONObject cannot be converted to JSONArray -


i'm new in android. i'm trying json got error. trying following tutorial http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

i search solution, still don't how use jsonarray.

here json example

[{"id":"152","category_id":"14","item_name":"restaurant1","cuisine_id":"3","cuisine_name":"chinese"},{"id":"161","category_id":"14","item_name":"restaurant10","cuisine_id":"17","cuisine_name":"middle eastern"},{"id":"153","category_id":"14","item_name":"restaurant2","cuisine_id":"17","cuisine_name":"middle eastern"},{"id":"154","category_id":"14","item_name":"restaurant3","cuisine_id":"7","cuisine_name":"american"},{"id":"155","category_id":"14","item_name":"restaurant4","cuisine_id":"3","cuisine_name":"chinese"},{"id":"156","category_id":"14","item_name":"restaurant5","cuisine_id":"8","cuisine_name":"coffee"},{"id":"157","category_id":"14","item_name":"restaurant6","cuisine_id":"8","cuisine_name":"coffee"},{"id":"158","category_id":"14","item_name":"restaurant7","cuisine_id":"17","cuisine_name":"middle eastern"},{"id":"159","category_id":"14","item_name":"restaurant8","cuisine_id":"6","cuisine_name":"indonesian"},{"id":"160","category_id":"14","item_name":"restaurant9","cuisine_id":"3","cuisine_name":"chinese"}] 

and here class

/**  * async task class json making http call  * */ private class getrestaurant extends asynctask<void, void, void> {      @override     protected void onpreexecute() {         super.onpreexecute();         // showing progress dialog         pdialog = new progressdialog(attractions.this);         pdialog.setmessage("please wait...");         pdialog.setcancelable(false);         pdialog.show();      }      @override     protected void doinbackground(void... arg0) {         // creating service handler class instance         servicehandler sh = new servicehandler();          // making request url , getting response         string jsonstr = sh.makeservicecall(url, servicehandler.get);          log.d("response: ", "> " + jsonstr);          if (jsonstr != null) {             try {                 jsonobject jsonobj = new jsonobject(jsonstr);                  // getting json array node                 restaurant = jsonobj.getjsonarray(tag_item_name);                  // looping through contacts                 (int = 0; < restaurant.length(); i++) {                     jsonobject c = restaurant.getjsonobject(i);                      string id = c.getstring(tag_cuisine_name);                      // tmp hashmap single contact                     hashmap<string, string> contact = new hashmap<string, string>();                      // adding each child node hashmap key => value                     contact.put(tag_cuisine_name, id);                       // adding contact contact list                     restaurantlist.add(contact);                 }             } catch (jsonexception e) {                 e.printstacktrace();             }         } else {             log.e("servicehandler", "couldn't data url");         }          return null;     }      @override     protected void onpostexecute(void result) {         super.onpostexecute(result);         // dismiss progress dialog         if (pdialog.isshowing())             pdialog.dismiss();         /**          * updating parsed json data listview          * */         listadapter adapter = new simpleadapter(                 attractions.this, restaurantlist,                 r.layout.attractionslayout, new string[] { tag_item_name, tag_cuisine_name }, new int[] { r.id.item_name,                         r.id.cuisine_name});       // assign adapter listview         listview.setadapter(adapter);     }  } 

thanks before.

try this..

your response starts jsonarray

[                ==> jsonarray     {            ==> jsonobject 

change this..

jsonobject jsonobj = new jsonobject(jsonstr);  // getting json array node restaurant = jsonobj.getjsonarray(tag_item_name); 

to

restaurant = new jsonarray(jsonstr); 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -