i have made php code array of data mysql struggling android , toast it.
here php code:
if($_server['request_method']=='get'){ $user_id = $_get['user_id']; require_once('dbconnect.php'); $user_tags = array(); foreach ($_request['user_id'] $key => $val) { $user_tags[$key] = filter_var($val, filter_sanitize_string); } $user_ids = "'" . implode("','", $user_tags) . "'"; $sql = "select * user_tags user_id in ({$user_ids})"; $r = mysqli_query($con,$sql); //creating blank array $result = array(); //looping through records fetched while($row = mysqli_fetch_array($r)){ $result['tags'][] = $row['tags']; } //displaying array in json format echo json_encode(array('result'=>$result)); mysqli_close($con); }
here url code:"http://allwaysready.16mb.com/cuboid/tagstest.php?user_id[]=7"
here result php code: {"result":{"tags":["pascol","php","python"]}}
i want "result" toasted in android.
here android code:
public class jobs extends appcompatactivity { public static string tags; private string json_string; public static final string tag_json_array="result"; public static final string key_user_tags="tags"; public static final string url_get_tag = "http://allwaysready.16mb.com/cuboid/tagstest.php?user_id[]="; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_jobs); getjson(); toast.maketext(getapplicationcontext(),tags,toast.length_long).show(); } private void showemployee() { jsonobject jsonobject = null; arraylist<hashmap<string, string>> list = new arraylist<hashmap<string, string>>(); try { jsonobject = new jsonobject(json_string); jsonarray result = jsonobject.getjsonarray(tag_json_array); (int = 0; < result.length(); i++) { jsonobject jo = result.getjsonobject(i); tags = jo.getstring(key_user_tags); hashmap<string, string> employees = new hashmap<>(); employees.put(key_user_tags, tags); } } catch (jsonexception e) { e.printstacktrace(); } } private void getjson(){ class getjson extends asynctask<void,void,string>{ progressdialog loading; @override protected void onpreexecute() { super.onpreexecute(); loading = progressdialog.show(jobs.this,"fetching data","wait...",false,false); } @override protected void onpostexecute(string s) { super.onpostexecute(s); loading.dismiss(); json_string = s; showemployee(); } @override protected string doinbackground(void... params) { requesthandler rh = new requesthandler(); string s = rh.sendgetrequest(url_get_tag); return s; } } getjson gj = new getjson(); gj.execute(); }
but blank toast . me guys if had made mistakes.
modify showemployee this:
private void showemployee() { jsonobject jsonobject = null; arraylist<hashmap<string, string>> list = new arraylist<hashmap<string, string>>(); try { jsonobject = new jsonobject(json_string); jsonobject result = jsonobject.getjsonobject(tag_json_array); jsonarray jsontags = result.getjsonarray(key_user_tags); (int = 0; < jsontags.length(); i++) { tags = tags + jsontags.getstring(i); hashmap<string, string> employees = new hashmap<>(); employees.put(key_user_tags, jsontags.getstring(i)); } } catch (jsonexception e) { e.printstacktrace(); } }
and set tags string empty string (else first object null)
public static string tags = "";
the code still has logic flaws, employees hashmap contain last entry, because creating new hashmap.
Comments
Post a Comment