android - org.json.JSONException: Value [....] of type org.json.JSONArray cannot be converted to JSONObject -
android - org.json.JSONException: Value [....] of type org.json.JSONArray cannot be converted to JSONObject -
got stuck error..:( here code.. json exception cannot converted jsonobject..
this furniture.java file. main file executed first want show layout listview shows logo, piece of furniture name , price.
public class piece of furniture extends activity { textview login; listview list; button b,search; string s,s1="error"; int value; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.furniture); string strurl = "http://realroom.byethost24.com/realroom/furniture.php" log.d("c1 :", s1); downloadtask downloadtask = new downloadtask(); downloadtask.execute(strurl); list = (listview) findviewbyid(r.id.flist); } public boolean oncreateoptionsmenu(menu menu) { // todo auto-generated method stub super.oncreateoptionsmenu(menu); menuinflater blowup = getmenuinflater(); blowup.inflate(r.menu.menu, menu); homecoming true; } @override public boolean onoptionsitemselected(menuitem item) { // todo auto-generated method stub intent i; switch(item.getitemid()) { case r.id.checkout: = new intent(furniture.this, payment.class); startactivity(i); break; case r.id.aboutus: = new intent(furniture.this,aboutus.class); startactivity(i); break; case r.id.profile: = new intent(furniture.this,profile.class); startactivity(i); break; case r.id.help: = new intent(furniture.this,help.class); startactivity(i); break; } homecoming false; } private string downloadurl(string strurl) throws ioexception { string info = ""; inputstream istream = null; seek { log.d("c2",s1); url url = new url(strurl); httpurlconnection urlconnection = (httpurlconnection) url.openconnection(); urlconnection.connect(); istream = urlconnection.getinputstream(); bufferedreader br = new bufferedreader(new inputstreamreader(istream)); stringbuffer sb = new stringbuffer(); string line = ""; while((line=br.readline())!=null) { sb.append(line); } info = sb.tostring(); br.close(); } catch(exception e) { log.e("c3",s1); log.d("exception while downloading url", e.tostring()); } { istream.close(); } homecoming data; } /** asynctask download json info */ private class downloadtask extends asynctask<string, integer, string> { string info = null; @override protected string doinbackground(string... url) { seek { info = downloadurl(url[0]); log.d("c26",s1); system.out.print(data); } catch(exception e) { log.d("background task",e.tostring()); log.e("c6",s1); } homecoming data; } @override protected void onpostexecute(string result) { // parsing of xml info done in non-ui thread listviewloadertask listviewloadertask = new listviewloadertask(); // start parsing xml info listviewloadertask.execute(result); } } /** asynctask parse json info , load listview */ private class listviewloadertask extends asynctask<string, void, simpleadapter> { jsonobject jobject; // doing parsing of xml info in non-ui thread @override protected simpleadapter doinbackground(string... strjson) { seek { log.d("c4",s1); jobject = new jsonobject(strjson[0]); log.d("c44",s1); furniturejsonparser furniturejsonparser = new furniturejsonparser(); log.d("c84",s1); furniturejsonparser.parse(jobject); log.d("c94",s1); } catch(exception e) { log.d("json exception1",e.tostring()); log.e("c5",s1); } // instantiating json parser class furniturejsonparser furniturejsonparser = new furniturejsonparser(); // list object store parsed countries list list<hashmap<string, object>> furnitures = null; seek { // getting parsed info list build furnitures = furniturejsonparser.parse(jobject); } catch(exception e) { log.d("exception",e.tostring()); log.e("c7",s1); } // keys used in hashmap string[] = { "furniture","flag","details"}; // ids of views in listview_layout int[] = { r.id.tv_fur,r.id.iv_icon,r.id.tv_fur_details}; // instantiating adapter store each items // r.layout.listview_layout defines layout of each item simpleadapter adapter = new simpleadapter(getbasecontext(), furnitures, r.layout.f_item, from, to); homecoming adapter; } /** invoked android on "doinbackground" executed */ @override protected void onpostexecute(simpleadapter adapter) { // setting adapter listview list.setadapter(adapter); log.d("c8",s); for(int i=0;i<adapter.getcount();i++) { hashmap<string, object> hm = (hashmap<string, object>) adapter.getitem(i); string imgurl = (string) hm.get("flag_path"); imageloadertask imageloadertask = new imageloadertask(); hashmap<string, object> hmdownload = new hashmap<string, object>(); hm.put("flag_path",imgurl); hm.put("position", i); log.d("c9",s1); // starting imageloadertask download , populate image in listview imageloadertask.execute(hm); } } } /** asynctask download , load image in listview */ private class imageloadertask extends asynctask<hashmap<string, object>, void, hashmap<string, object>>{ @override protected hashmap<string, object> doinbackground(hashmap<string, object>... hm) { inputstream istream=null; log.d("c10",s1); string imgurl = (string) hm[0].get("flag_path"); int position = (integer) hm[0].get("position"); url url; log.d("c11",s1); seek { url = new url(imgurl); // creating http connection communicate url httpurlconnection urlconnection = (httpurlconnection) url.openconnection(); // connecting url urlconnection.connect(); // reading info url istream = urlconnection.getinputstream(); // getting caching directory file cachedirectory = getbasecontext().getcachedir(); log.d("c12",s1); // temporary file store downloaded image file tmpfile = new file(cachedirectory.getpath() + "/wpta_"+position+".jpeg"); log.d("c13",s1); // fileoutputstream temporary file fileoutputstream foutstream = new fileoutputstream(tmpfile); // creating bitmap downloaded inputstream bitmap b = bitmapfactory.decodestream(istream); // writing bitmap temporary file png file b.compress(bitmap.compressformat.jpeg,100, foutstream); // flush fileoutputstream foutstream.flush(); //close fileoutputstream foutstream.close(); // create hashmap object store image path , position in listview hashmap<string, object> hmbitmap = new hashmap<string, object>(); // storing path temporary image file hmbitmap.put("flag",tmpfile.getpath()); // storing position of image in listview hmbitmap.put("position",position); // returning hashmap object containing image path , position homecoming hmbitmap; } grab (exception e) { e.printstacktrace(); log.e("c14",s1); } homecoming null; } @override protected void onpostexecute(hashmap<string, object> result) { log.d("c15",s1); // getting path downloaded image string path = (string) result.get("flag"); // getting position of downloaded image int position = (integer) result.get("position"); // getting adapter of listview simpleadapter adapter = (simpleadapter ) list.getadapter(); // getting hashmap object @ specified position of listview hashmap<string, object> hm = (hashmap<string, object>) adapter.getitem(position); // overwriting existing path in adapter hm.put("flag",path); // noticing listview dataset changes adapter.notifydatasetchanged(); } } }
furniturejsonparser.java file
this file used parse info via json
class furniturejsonparser { string s="error"; public list<hashmap<string,object>> parse(jsonobject jobject) { jsonarray jfurnitures = null; seek { jfurnitures = jobject.getjsonarray("furnitures"); } catch(jsonexception e) { e.printstacktrace(); } homecoming getfurnitures(jfurnitures); } private list<hashmap<string, object>> getfurnitures(jsonarray jfurnitures) { int furniturecount = jfurnitures.length(); list<hashmap<string, object>> furniturelist = new arraylist<hashmap<string,object>>(); hashmap<string, object> piece of furniture = null; for(int i=0; i<furniturecount;i++) { seek { piece of furniture = getfurniture((jsonobject)jfurnitures.get(i)); furniturelist.add(furniture); log.d("c18",s); } grab (jsonexception e) { e.printstacktrace(); log.e("c19",s); } } homecoming furniturelist; } private hashmap<string, object> getfurniture(jsonobject jfurniture) { hashmap<string, object> piece of furniture = new hashmap<string, object>(); string furniturename = ""; string flag=""; string language = ""; seek { furniturename = jfurniture.getstring("furniture_name"); flag = jfurniture.getstring("furniture_image"); language = jfurniture.getstring("furniture_cost"); string details = "language : " + language + "\n" furniture.put("furniture", furniturename); furniture.put("flag", r.drawable.blank); furniture.put("flag_path", flag); furniture.put("details", details); log.d("c20",s); } grab (jsonexception e) { e.printstacktrace(); log.e("c21",s); } homecoming furniture; } } furniture.php file
the php file fetch info database , send android
<?php include('config.php'); date_default_timezone_set("asia/calcutta"); $result1 = mysqli_query($con,"select furniture_name, furniture_image, furniture_cost tbl_furniture"); while($row=mysqli_fetch_array($result1)) { //$row['store_name']; //echo"<br>"; $back_android[]=$row; } print(json_encode($back_android)); ?> furniture.xml file
the layout of piece of furniture file
<?xml version="1.0" encoding="utf-8"?> <scrollview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical" tools:context=".mainactivity" > <linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/bg" > <button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/search" android:layout_gravity="center" android:text="search furniture" android:textsize="10dp" android:textcolor="#aaaaaa" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/sf" android:layout_gravity="center" android:text="select furniture" android:textsize="20dp" android:textcolor="#ffffff" /> <listview android:id="@+id/flist" android:layout_width="fill_parent" android:layout_height="402dp" android:layout_margintop="10dp" android:layout_weight="0.18" > </listview> </linearlayout> </scrollview> f_item.xml file
layout of listview in furniture.xml file
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <textview android:id="@+id/tv_fur" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:textsize="20dp" android:textstyle="bold" /> <imageview android:id="@+id/iv_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_fur" android:layout_centervertical="true" android:padding="5dp" android:contentdescription="@string/str_iv_flag" /> <textview android:id="@+id/tv_fur_details" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_torightof="@id/iv_icon" android:layout_below="@id/tv_fur" /> </relativelayout>
the next code homecoming list of furnitures based on json
private list<furniture> getthemfurnitures(string json){ list<furniture> furnitures = new arraylist<furniture>(); seek { jsonobject jsonroot = new jsonobject(json); jsonarray jsonarray = jsonroot.getjsonarray("furnitures"); for(int = 0; < jsonarray.length(); i++){ jsonobject furnitureobject = jsonarray.getjsonobject(i); string storeid = furnitureobject.getstring("store_id"); string name = furnitureobject.getstring("furniture_name"); string cost = furnitureobject.getstring("furniture_cost"); string image = furnitureobject.getstring("furniture_image"); furnitures.add(new furniture(storeid, name, cost, image)); } } grab (jsonexception e) { e.printstacktrace(); } homecoming furnitures; } android jsonexception
Comments
Post a Comment