java - Error on fetching data from Mysql DB with JSON and PHP -
java - Error on fetching data from Mysql DB with JSON and PHP -
i learning build android applications , need help whit getting info mysql. followed this tutorial error:
error org.json.jsonexception: value of type java.lang.string cannot converted jsonobject
on mainactivity have 1 button when clicked open sec activity. sec activity must , show info db. code of sec activity restaurant.java
public class restaurants extends activity { private string jsonresult; private string url = "http://10.0.2.2/app1/getrestaurants.php"; private listview listview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.restaurants); listview = (listview) findviewbyid(r.id.listview1); accesswebservice(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); homecoming true; } // async task access web private class jsonreadtask extends asynctask<string, void, string> { @override protected string doinbackground(string... params) { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(params[0]); seek { httpresponse response = httpclient.execute(httppost); jsonresult = inputstreamtostring( response.getentity().getcontent()).tostring(); } grab (clientprotocolexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } homecoming null; } private stringbuilder inputstreamtostring(inputstream is) { string rline = ""; stringbuilder reply = new stringbuilder(); bufferedreader rd = new bufferedreader(new inputstreamreader(is)); seek { while ((rline = rd.readline()) != null) { answer.append(rline); } } grab (ioexception e) { // e.printstacktrace(); toast.maketext(getapplicationcontext(), "error..." + e.tostring(), toast.length_long).show(); } homecoming answer; } @override protected void onpostexecute(string result) { listdrwaer(); } }// end async task public void accesswebservice() { jsonreadtask task = new jsonreadtask(); // passes values urls string array task.execute(new string[] { url }); } // build hash set list view public void listdrwaer() { list<map<string, string>> restaurantlist = new arraylist<map<string, string>>(); seek { jsonobject jsonresponse = new jsonobject(jsonresult); jsonarray jsonmainnode = jsonresponse.optjsonarray("restoranti"); (int = 0; < jsonmainnode.length(); i++) { jsonobject jsonchildnode = jsonmainnode.getjsonobject(i); string name = jsonchildnode.optstring("name"); string menu = jsonchildnode.optstring("menu"); string output = name + "-" + menu; restaurantlist.add(createrestaurants("restoranti", output)); } } grab (jsonexception e) { toast.maketext(getapplicationcontext(), "error " + e.tostring(), toast.length_short).show(); } simpleadapter simpleadapter = new simpleadapter(this, restaurantlist, android.r.layout.simple_list_item_1, new string[] { "restoranti" }, new int[] { android.r.id.text1 }); listview.setadapter(simpleadapter); } private hashmap<string, string> createrestaurants(string name, string menu) { hashmap<string, string> restaurantnameno = new hashmap<string, string>(); restaurantnameno.put(name, menu); homecoming restaurantnameno; } }
this restaurants.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <listview android:id="@+id/listview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="14dp" > </listview>
please allow me know if need post more code. many thank's help.
i found this answer post similar , tried create same didn't work me. same error , blank page.
// maybe help ... private class jsonreadtask extends asynctask<string, void, string> { @override protected string doinbackground(string... params) { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(params[0]); seek { httpresponse response = httpclient.execute(httppost); jsonresult = inputstreamtostring( response.getentity().getcontent()).tostring(); // lets homecoming someting ... homecoming jsonresult; } grab (clientprotocolexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } homecoming null; }
java android mysql json
Comments
Post a Comment