php - Android external online database -
i have online database in phpmyadmin. @ time can query him , can result in textview want value of each cell, how i?
this example
[{"monday":"","tuesday":"","wednesday":"","thursday":"","friday":"","saturday":"","sunday":"","id":"","info":""},...]
i value of monday example.
thanks
code sendquery.java:
public class sendquery { /////////// public method send query /////////// public static string send(string query) { string result = "0"; inputstream = null; string weekdayval; //the query send arraylist<namevaluepair> querysend = new arraylist<namevaluepair>(); querysend.add(new basicnamevaluepair("querysend",query)); //http post try{ httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://locali.altervista.org/php/locali.php"); httppost.setentity(new urlencodedformentity(querysend)); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); }catch(exception e){ log.e("log_tag", "error in http connection "+e.tostring()); } //convert response string 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(); result=sb.tostring(); try{ jsonarray weekdetails = new jsonarray ( result); // response string for(int index=0;index < weekdetails.length();index++) { jsonobject tempweekdetail = weekdetails.getjsonobject(index); weekdayval = tempweekdetail.getstring("monday");// value monday //added log can view logcat. changed above variable name log.i("resp value"," moday value"+weekdayval); } }catch(exception e) { //catch exception here } }catch(exception e){ log.e("log_tag", "error converting result: "+e.tostring()); } log.i("sendquery", result); return result; }
}
main.java:
public class main extends activity { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); textview reslayout = (textview) findviewbyid(r.id.res); string res = sendquery.send("select * contatti"); reslayout.append(res); } }
you can try following code.
/*string res = "[{\"monday\":\"work\",\"tuesday\":\"\",\"wednesday\":\"\",\"thursday\":\"\",\"friday\":\"\",\"saturday\":\"\",\"sunday\":\"\",\"id\":\"\",\"info\":\"\"}]";//you have escape double quotes*/ try{ jsonarray weekdetails = new jsonarray ( result); // response string for(int index=0;index < weekdetails.length();index++) { jsonobject tempweekdetail = weekdetails.getjsonobject(index); string weekdayval = tempweekdetail.getstring("monday");// value monday //added log can view logcat. changed above variable name log.i("resp value"," moday value"+weekdayval); } }catch(exception e) { //catch exception here }
Comments
Post a Comment