php - I want to show names of people on the listview in android through json -
i using asynctask through want json array of names , want show in listview . here php code:
$con=mysqli_connect("localhost","root","","raheel"); $id=intval($_post['id']); $mysqli_qry="select* name name_id='$id'; "; $result=mysqli_query($con,$mysqli_qry)or die("error occur in connection" . mysqli_error($con)); $rowcount=mysqli_num_rows($result); $arr=array(); if($rowcount>0) { while($row=mysqli_fetch_assoc($result)) { $arr=$row; } } header('content-type: application/json'); echo json_encode($arr); mysqli_close($con ?>
the problem facing how json array in asynctask mode , assign data(names) listview home.java code: package com.example.raheel.bank;
public class home extends appcompatactivity implements homecallback { listview listview; private listadapter madapter; p public arraylist<string > info; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_home); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); listview=(listview)findviewbyid(r.id.listview); int id=0; } backgroundhome home=new backgroundhome(home.this, (callback)home. this); @override public void processdata(jsonarray data) { if(data!=null) { info=new arraylist<>(); try { for(int i=0;i<data.length();i++) { jsonobject js=data.getjsonobject(i); } } catch (jsonexception e) { e.printstacktrace(); } } madapter=new arrayadapter<string>(this,android.r.layout.simple_list_item_1,info); listview.setadapter(madapter); }
}
class backgroundhome extends asynctask<integer,integer,jsonarray>{ context c; callback callback; public backgroundhome(context c, callback callback) { this.c = c; this.callback = callback; } @override protected jsonarray doinbackground(integer...params) { jsonarray js=null; string url=c.getstring(r.string.url) +"home.php"; try { httprequest httprequest = new httprequest(url); hashmap<string, string> values = new hashmap<>(); values.put("id", integer.tostring(params[0])); js = httprequest.prepare(httprequest.method.post).withdata(values).sendandreadjsonarray(); } catch (ioexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } return js; } protected void onpostexecute(jsonarray jsonarray){ homecallback.processdata(jsonarray); } } interface homecallback{ public void processdata(jsonarray data); }
this home.java file dnt know how json array of names php file.
you have add data array this. modifying $arr = $row $arr[] = .. give result array key/value data. json_encode later in code convert json array needed application.
while($row=mysqli_fetch_assoc($result)) { $arr[] = array('name' => $row["name"], 'id' => $row["name_id"]); }
Comments
Post a Comment