php - Cannot list the data in my Aandroid application from Mysql -
am new in android development. trying database crud operation in android application. can insert data mysql.am using php , json parsing it.when data inserted database, next activity should listing data.the problem no data coming listing activity. activity coming without data. kindly me .
here in code , trying display 3 data , "name, price, description. php file retrieve data database mysql , , convert strings json objects. javacode call json class , list data. can insert data mysql.am using php , json parsing it.when data inserted database, next activity should listing data.the problem no data coming listing activity. activity coming without data. java code listing products is
public class allproductsactivity extends listactivity {
// progress dialog private progressdialog pdialog; // creating json parser object jsonparser jparser = new jsonparser(); arraylist<hashmap<string, string>> productslist; // url products list private static string url_all_products = "http://10.0.2.2/get_all_products.php"; // json node names private static final string tag_success = "success"; private static final string tag_products = "products"; private static final string tag_pid = "pid"; private static final string tag_name = "name"; // products jsonarray jsonarray products = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.all_products); // hashmap listview productslist = new arraylist<hashmap<string, string>>(); // loading products in background thread new loadallproducts().execute(); // listview listview lv = getlistview(); // on seleting single product // launching edit product screen lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // getting values selected listitem string pid = ((textview) view.findviewbyid(r.id.pid)).gettext() .tostring(); // starting new intent intent in = new intent(getapplicationcontext(), editproductactivity.class); // sending pid next activity in.putextra(tag_pid, pid); // starting new activity , expecting response startactivityforresult(in, 100); } }); } // response edit product activity @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); // if result code 100 if (resultcode == 100) { // if result code 100 received // means user edited/deleted product // reload screen again intent intent = getintent(); finish(); startactivity(intent); } } /** * background async task load product making http request * */ class loadallproducts extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(allproductsactivity.this); pdialog.setmessage("loading products. please wait..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } /** * getting products url * */ protected string doinbackground(string... args) { // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); // getting json string url jsonobject json = jparser.makehttprequest(url_all_products, "get", params); // check log cat json reponse log.d("all products: ", json.tostring()); try { // checking success tag int success = json.getint(tag_success); if (success == 1) { // products found // getting array of products products = json.getjsonarray(tag_products); // looping through products (int = 0; < products.length(); i++) { jsonobject c = products.getjsonobject(i); // storing each json item in variable string id = c.getstring(tag_pid); string name = c.getstring(tag_name); // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); // adding each child node hashmap key => value map.put(tag_pid, id); map.put(tag_name, name); // adding hashlist arraylist productslist.add(map); } } else { // no products found // launch add new product activity intent = new intent(getapplicationcontext(), newproductactivity.class); // closing previous activities i.addflags(intent.flag_activity_clear_top); startactivity(i); } } catch (jsonexception e) { e.printstacktrace(); } return null; } /** * after completing background task dismiss progress dialog * **/ protected void onpostexecute(string file_url) { // dismiss dialog after getting products pdialog.dismiss(); // updating ui background thread runonuithread(new runnable() { public void run() { /** * updating parsed json data listview * */ listadapter adapter = new simpleadapter( allproductsactivity.this, productslist, r.layout.list_item, new string[] { tag_pid, tag_name}, new int[] { r.id.pid, r.id.name }); // updating listview setlistadapter(adapter); } }); } } } here in code , trying display 3 data , "name, price, description. php file retrieve data database mysql , , convert strings json objects. javacode call json class , list data. can insert data mysql.am using php , json parsing it.when data inserted database, next activity should listing data.the problem no data coming listing activity. activity coming without data. kindly me . json class parsing or converting follows json parsing code .in code first created function json url . code making http request , post method using if , else statement . in else method put code method . converted json object , , above android code call , display in activity.. used json parser class json url. class supports 2 http request methods , post json url. getting json making http call, adding async method make http calls on background thread. add follwing method in main activity class public jsonparser() { } // function json url // making http post or mehtod public jsonobject makehttprequest(string url, string method, list<namevaluepair> params) { // making http request try { // check request method if(method == "post"){ // request method post // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params)); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); }else if(method == "get"){ // request method defaulthttpclient httpclient = new defaulthttpclient(); string paramstring = urlencodedutils.format(params, "utf-8"); url += "?" + paramstring; httpget httpget = new httpget(url); httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); json = sb.tostring(); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try { if(isvalidjson(json)) jobj = new jsonobject(json); } catch (jsonexception e) { log.v("json string","result => "+json); } // return json string return jobj; } } json parsing code .in code first created function json url . code making http request , post method using if , else statement . in else method put code method . converted json object , , above android code call , display in activity.. used json parser class json url. class supports 2 http request methods , post json url. getting json making http call, adding async method make http calls on background thread. used json parser class json url. class supports 2 http request methods , post json url. getting json making http call, adding async method make http calls on background thread. since nothing displayed in activity. have following error in logcat. logcat error displayed below .
since nothing displayed in activity. have following error in logcat. logcat error displayed below
. since nothing displayed in activity. have following error in logcat. logcat error displayed below
03-10 03:49:54.491: w/system.err(1559): org.json.jsonexception: no value products 03-10 03:49:54.491: w/system.err(1559): @ org.json.jsonobject.get(jsonobject.java:354) 03-10 03:49:54.508: i/choreographer(1559): skipped 97 frames! application may doing work on main thread. 03-10 03:49:54.528: w/system.err(1559): @ org.json.jsonobject.getjsonarray(jsonobject.java:548) 03-10 03:49:54.558: w/system.err(1559): @ com.example.sampleapp2.allproductsactivity$loadallproducts.doinbackground(allproductsactivity.java:141) 03-10 03:49:54.618: w/system.err(1559): @ com.example.sampleapp2.allproductsactivity$loadallproducts.doinbackground(allproductsactivity.java:1) 03-10 03:49:54.668: w/system.err(1559): @ android.os.asynctask$2.call(asynctask.java:287) 03-10 03:49:54.698: w/system.err(1559): @ java.util.concurrent.futuretask.run(futuretask.java:234) 03-10 03:49:54.718: w/system.err(1559): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 03-10 03:49:54.748: w/system.err(1559): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 03-10 03:49:54.788: w/system.err(1559): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 03-10 03:49:54.898: i/choreographer(1559): skipped 84 frames! application may doing work on main thread. 03-10 03:49:54.958: w/system.err(1559): @ java.lang.thread.run(thread.java:841) 03-10 03:49:55.038: i/choreographer(1559): skipped 83 frames! application may doing work on main thread.a`enter code here` 03-10 03:49:55.168: i/choreographer(1559): skipped 212 frames! application may doing work on main thread. 03-10 03:49:55.218: i/choreographer(1559): skipped 55 frames! application may doing work on main thread.
check jsonobject has products's key value.
03-10 03:49:54.491: w/system.err(1559): org.json.jsonexception: no value products
this error show's jsonobject not have products key value.
Comments
Post a Comment