android - org.json.JSONException:Value of type java.lang.String cannot be converted to JSONArray -
i'm building application in i'm receiving jsonarray server , i'm using volley library send , receive response server when click on button send i'm getting value of type java.lang.string cannot converted jsonarray
in toast
[{"username":"ude","password":"hello"}]
login.java
public class login extends activity { button log; edittext username, passsword; string user, pass; string tag_json_arry = "jarray_req"; private string jsonresponse; private static string tag = login.class.getsimplename(); protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.login); username = (edittext) findviewbyid(r.id.username); passsword = (edittext) findviewbyid(r.id.password); log = (button) findviewbyid(r.id.login); log.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { user = username.gettext().tostring().trim(); pass = passsword.gettext().tostring().trim(); makejsonobjreq(); } }); private void makejsonobjreq() { jsonarrayrequest jsonarrayreq = new jsonarrayrequest(request.method.post, const.url_json_object, null, new response.listener<jsonarray>() { @override public void onresponse(jsonarray response) { log.d(tag, response.tostring()); try { // parsing json array response // loop through each json object jsonresponse = ""; (int = 0; < response.length(); i++) { jsonobject person = (jsonobject) response .get(i); string name = person.getstring("username"); string password = person.getstring("password"); jsonresponse += "name: " + name + "\n\n"; jsonresponse += "password: " + password + "\n\n"; } toast.maketext(getapplicationcontext(), "" + jsonresponse, toast.length_long).show(); } catch (jsonexception e) { e.printstacktrace(); toast.maketext(getapplicationcontext(), "error: " + e.getmessage(), toast.length_long).show(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { volleylog.d(tag, "error: " + error.getmessage()); toast.maketext(login.this, "" + error.getmessage(), toast.length_long).show(); } }) { /** * passing request headers * */ @override public map<string, string> getheaders() throws authfailureerror { hashmap<string, string> headers = new hashmap<>(); headers.put("content-type", "application/json"); return headers; } @override protected map<string, string> getparams() { map<string, string> params = new hashmap<>(); params.put("username", user); params.put("password", pass); return params; } }; // adding request request queue appcontroller.getinstance().addtorequestqueue(jsonarrayreq, tag_json_arry); // cancelling request // applicationcontroller.getinstance().getrequestqueue().cancelall(tag_json_obj); }
php:
<?php require_once "connect.php"; $username = $_request["username"]; $password = $_request["password"]; $return = []; $stmt = $pdo->prepare("select * users username = :username , password = :password limit 1"); $stmt->bindparam(":username", $username); $stmt->bindparam(":password",$password); $stmt->execute(); $result = $stmt->fetchall(pdo::fetch_assoc); if(!$result){ echo json_encode("fail"); } else{ // array json response $response = array(); $response["users"] = array(); $users= array(); $users["username"] = $result["username"]; $users["password"] = $result["password"]; array_push($response["apps"], $users); $response["success"] = 1; echo json_encode($response); } $pdo = null; ?>
try :
public void onresponse(jsonarray response) { log.d(tag, response.tostring()); try { // parsing json array response // loop through each json object string responsestring = response.tostring(); jsonarray jarray = new jsonarray(responsestring); (int = 0; < jarray.length(); i++) { jsonobject person = jarray.getjsonobject(i); string name = person.getstring("username"); string password = person.getstring("password"); jsonresponse += "name: " + name + "\n\n"; jsonresponse += "password: " + password + "\n\n"; }
Comments
Post a Comment