php - Value success of type java.lang.String cannot be converted to JSONObject -
i have listview in activity a. when long press detected, check whether id maximum. if yes, list deleted , list refreshed. otherwise display "list cannot deleted".
listviewupdate.setonitemlongclicklistener(new adapterview.onitemlongclicklistener() { public boolean onitemlongclick(final adapterview<?> p, view v, final int po, long id) { id = details1.get(po).getid(); alertdialog.builder builder = new alertdialog.builder(getactivity()); builder.settitle("delete"); builder.setmessage("are sure want delete?"); builder.seticon(android.r.drawable.ic_dialog_alert); builder.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int ii) { checkmaxid(po,id,id); retrievetotalhours(id); } }); builder.setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int ii) { dialog.dismiss(); } } ); builder.show(); return true; } }); return edit_details; } public void checkmaxid(final int po,final int foreignkey,final string id) { class check extends asynctask<void,void,string>{ // progressdialog loading; @override protected void onpreexecute() { super.onpreexecute(); // loading = progressdialog.show(edit_staff.this,"updating...","wait...",false,false); } @override protected void onpostexecute(string s) { super.onpostexecute(s); toast.maketext(getactivity(), s, toast.length_long).show(); try{ jsonobject json = new jsonobject(s); if(json.getboolean("success")){ objadapter.removeitem(po); objadapter.notifydatasetchanged(); toast.maketext(getactivity(), json.getstring("message"), toast.length_long).show(); log.e("list", "deleted"); }else{ toast.maketext(getactivity(), json.getstring("message"), toast.length_long).show(); log.e("list", " not deleted"); } }catch(jsonexception ex){ ex.printstacktrace(); log.e("lisdsss","ex="+ex.tostring()); } } @override protected string doinbackground(void... params) { hashmap<string,string> hashmap = new hashmap<>(); hashmap.put(configs.key_id, id); hashmap.put(configs.key_twd, string.valueof(foreignkey)); requesthandler rh = new requesthandler(); string s = rh.sendpostrequest(configs.url_check_id, hashmap); return s; } } check ue = new check(); ue.execute(); } php
<?php $json = array(); if(isset($_post['id'], $_post['twd'])){ /*importing our db connection script*/ require_once('dbconnect.php'); /* check connection */ if (mysqli_connect_errno()) { printf("connect failed: %s\n", mysqli_connect_error()); exit(); } $id = mysqli_real_escape_string($con, $_post['id']); $twd = mysqli_real_escape_string($con, $_post['twd']); $sql ="select max(id) maxid work_details twd = '$twd'"; if ($result = mysqli_query($con, $sql)) { /* fetch associative array */ if ($row = mysqli_fetch_row($result)) { if($row[0] === $id){ $sql ="delete work_details id='$id';"; if ($result = mysqli_query($con, $sql)) { echo 'success'; $json['success'] = true; $json['message'] = 'delete successful'; }else{ $json['success'] = false; $json['message'] = 'list cannot deleted'; } }else echo 'list cannot deleted'; $json['success'] = false; $json['message'] = '($row[0] !== $id)'; } } else{ $json['success'] = false; $json['message'] = 'select unsuccessful'; } /* close connection */ mysqli_close($con); } ?> the list has maximum id deleted in mysql, problem list not refreshing.
logcat
e/lisdsss﹕ ex=org.json.jsonexception: value success of type java.lang.string cannot converted jsonobject this ex.printstacktrace();, in catch block.
any appreciated.
there 2 methods solve this. method feel better or comfortable. @nikiole91 pointed out remove echo php script. print json output.
php
<?php $json = array(); if(isset($_post['id'], $_post['twd'])){ /*importing our db connection script*/ require_once('dbconnect.php'); /* check connection */ if (mysqli_connect_errno()) { printf("connect failed: %s\n", mysqli_connect_error()); exit(); } $id = mysqli_real_escape_string($con, $_post['id']); $twd = mysqli_real_escape_string($con, $_post['twd']); $sql ="select max(id) maxid work_details twd = '$twd'"; if ($result = mysqli_query($con, $sql)) { /* fetch associative array */ if ($row = mysqli_fetch_row($result)) { if($row[0] === $id){ $sql ="delete work_details id='$id';"; if ($result = mysqli_query($con, $sql)) { $json['success'] = true; $json['message'] = 'delete successful'; }else{ $json['success'] = false; $json['message'] = 'list cannot deleted'; } }else $json['success'] = false; $json['message'] = '($row[0] !== $id)'; } } else{ $json['success'] = false; $json['message'] = 'select unsuccessful'; } /* close connection */ mysqli_close($con); } //print json echo json_encode($json); ?> now android code should work fine.
Comments
Post a Comment