i have made php code array of data mysql struggling android
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); }
this gives result:{"result":{"tags":["pascol","php","python"]}}
when try toast "tags" in android error:
rg.json.jsonexception: value {"tags":["pascol","php","python"]} @ result of type org.json.jsonobject cannot converted jsonarray
my android code:
public class jobs extends appcompatactivity { public static final string data_url = "http://allwaysready.16mb.com/sort.php?work[]="; public static string tags=""; private string json_string; public static final string tag_json_array="result"; public static final string key_user_tags="tags"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_jobs); getemployee(); toast.maketext(getapplicationcontext(),tags,toast.length_long).show(); toast.maketext(getapplicationcontext(),"hi",toast.length_long).show(); check(); } private void getemployee(){ class getemployee extends asynctask<void,void,string>{ progressdialog loading; @override protected void onpreexecute() { super.onpreexecute(); loading = progressdialog.show(jobs.this,"fetching...","wait...",false,false); } @override protected void onpostexecute(string s) { super.onpostexecute(s); loading.dismiss(); showemployee(s); } @override protected string doinbackground(void... params) { requesthandler rh = new requesthandler(); string s = rh.sendgetrequestparam(config.url_get_tag,userareaactivity.id); return s; } } getemployee ge = new getemployee(); ge.execute(); } private void showemployee(string json) { try { jsonobject jsonobject = new jsonobject(json); jsonarray result = jsonobject.getjsonarray(tag_json_array); jsonobject c = result.getjsonobject(0); string ttags = c.getstring(key_user_tags); tags=ttags.tostring(); } catch (jsonexception e) { e.printstacktrace(); } }
i think response coming jsonobject. trying convert jsonarray.
try this,
jsonobject valuejsn = new jsonobject(response); jsonarray tags =valuejsn.getjsonarray("tags");
Comments
Post a Comment