Android read data from php file -
i have application read data php file. try search in google how read data php , replace in textview when try make code nothing show in application.
my php code:
<?php $serverip = "127.0.0.1"; // server ip public $portzone = "27780"; // zoneserver port $portlogin = "10007"; // zoneserver port $file = file ("e:\server\zoneserver\systemsave\serverdisplay.ini"); foreach($file $line) { if(strspn($line, "[") != 1) parse_str($line); } $response["online"] = array(); $product["total online"] = $usernum; $product["acc online"] = $a_num; $product["bcc online"] = $b_num; $product["ccc online"] = $c_num; // push single product final response array array_push($response["online"], $product); // success $response["success"] = 1; echo json_encode($response); ?>
my code android
toolbar toolbar; textview onlineplayer; // progress dialog private progressdialog pdialog; // creating json parser object jsonparser jparser = new jsonparser(); private listview listview; arraylist<hashmap<string, string>> productslist; // url products list private static string url_all_products = "http://192.168.1.111/status_online.php"; // json node names private static final string tag_success = "success"; private static final string tag_products = "online"; private static final string tag_online = "total online"; private static final string tag_cpt = "pvppoint"; private static final string tag_race = "race"; // products jsonarray jsonarray products = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_status_server); // listview listview = (listview) findviewbyid(android.r.id.list); // hashmap listview productslist = new arraylist<hashmap<string, string>>(); // loading products in background thread new loadallproducts().execute(); toolbar = (toolbar) findviewbyid(r.id.toolbar); onlineplayer = (textview) findviewbyid(r.id.playeringame); setsupportactionbar(toolbar); getsupportactionbar().setdisplayhomeasupenabled(true); //listview = (listview) findviewbyid(r.id.lista); typedvalue typedvaluecolorprimarydark = new typedvalue(); statusserveractivity.this.gettheme().resolveattribute(r.attr.colorprimary, typedvaluecolorprimarydark, true); final int colorprimarydark = typedvaluecolorprimarydark.data; if (build.version.sdk_int >= 21) { getwindow().setstatusbarcolor(colorprimarydark); } } // response edit product activity @override public 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, void, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(statusserveractivity.this); pdialog.setmessage("loading. 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{ jsonobject obj = jparser.makehttprequest(url_all_products, "get", params); string temperature = obj.getstring("temperature"); textview tv = (textview)findviewbyid(r.id.playeringame); tv.settext("temperature: " + temperature); }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( // statusserveractivity.this, productslist, // r.layout.activity_status_server, new string[]{ // tag_online}, // new int[]{r.id.playeringame}); // updating listview //getlistview().setadapter(adapter); //} // }); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. menuinflater inflater = getmenuinflater(); getmenuinflater().inflate(r.menu.menu_news, menu); return super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); }
my php code run here: http://prntscr.com/bewhdd
how replace data php textview in application?
the problem is, trying value out of nested jsonobject directly without 'walking' way down.
since jsonobject looks this:
{'online': [ {'total online': '3',....} ], 'success': 1 }
you have do
jsonobject obj = jparser.makehttprequest(url_all_products, "get", params); string totalonline = obj.getjsonarray('online').getjsonobject(0).getstring('total online');
note, here i'm chaining functions. step step - if need reuse 1 of items:
jsonobject obj = jparser.makehttprequest(url_all_products, "get", params); jsonarray online = obj.getjsonarray('online'); jsonobject firstonlineobject = online.getjsonobject(0); string totalonline = firstonlineobject.getstring('total online');
Comments
Post a Comment