php - org.json.JSONException: Value Connected of type java.lang.String cannot be converted to JSONObject -
i'm trying list challenges external sql database. having big problem. retrieving data mysql ok, hitting errors on jsonobject. have been trying fair few hours if can point me in right direction - have been using tutorial reference https://www.simplifiedcoding.net/android-spinner-example-to-load-json-using-volley/
php file - challengesall.php
<?php $servername = "localhost"; $username = "user"; $password = "pass"; $dbname ="android_api"; // create connection $conn = new mysqli($servername, $username, $password, $dbname); // check connection if ($conn->connect_error) { die("connection failed: " . $conn->connect_error); } echo "connected successfully"; //$stmt = $dbconn->conn->prepare("select * challenges") or die(mysql(mysql_error())); $stmt = "select * challenges"; $result = $conn->query($stmt); $results = array(); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { array_push($results,array( 'unique_id'=>$row["unique_id"], 'title'=>$row["title"] )); // echo "id: " . $row["id"]. " - unique id: " . $row["unique_id"]. " - title: " . $row["title"]. "<br>"; } echo json_encode(array('results'=>$results)); } else { echo "0 results"; } $conn->close(); ?>
fields
string url = "http://mylocalipconnectsfine/android_login_api/challengesall.php"; public static final string json_array = "results"; private jsonarray challengeresult;
challengelistactivity - method getdata
private void getdata() { //tag used cancel request string tag_string_req = "req_challenge"; log.i("m4: " + tag + ":- " + step, thread.currentthread().getstacktrace()[2].getmethodname()); stringrequest strreq = new stringrequest( url, new response.listener<string>() { @override public void onresponse(string response) { log.i("m6: " + tag + ":- " + step, thread.currentthread().getstacktrace()[3].getmethodname()); log.d(tag, "login response: " + response.tostring()); jsonobject j = null; try { //parsing fetched json string json object j = new jsonobject(response); //!!goes catch exception!!! //storing array of json string our json array challengeresult = j.getjsonarray(json_array); //does not here!! :(( // todo: store user in sqlite loop //calling method getstudents students json array getchallenges(challengeresult); // } } catch (jsonexception e) { // json error e.printstacktrace(); toast.maketext(getapplicationcontext(), "json error: " + e.getmessage(), toast.length_long).show(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { toast.maketext(getapplicationcontext(), error.getmessage(), toast.length_long).show(); } }) ; // adding request request queue // appcontroller.getinstance().addtorequestqueue(strreq, tag_string_req); requestqueue requestqueue = volley.newrequestqueue(this); requestqueue.add(strreq); }
error message
07-25 16:54:09.900 8134-8134/boo.famvsfam d/steps: challengelistactivity: login response: connected successfully{"result":[{"unique_id":"57933c641237d4.96974874","title":"23072016"},{"unique_id":"57901472464398.57585757","title":"hayley"},{"unique_id":"5790142623de26.96306456","title":"nbppp"},{"unique_id":"5790133be27ca1.05397869","title":"bopo"},{"unique_id":"57900e98bc4dd2.35778585","title":"howdie"},{"unique_id":"57900dfb7fca74.47168154","title":"booo"},{"unique_id":"579006b05dfda0.42529504","title":"booo"},{"unique_id":"578f7e139835d2.76955191","title":"ghhg"},{"unique_id":"578f71b8ef5693.01745921","title":"howdie"},{"unique_id":"578f71b50c7407.91547319","title":"howdie"},{"unique_id":"578f4d212bcf41.00048708","title":"heloorout"},{"unique_id":"578f4b794281e1.43679386","title":"heloorout"},{"unique_id":"578ed1338cdfd2.06152669","title":"boo"},{"unique_id":"578e72b5920bd9.67743323","title":"boo3"},{"unique_id":"578e7168921b95.27637073","title":"boo4566677777"},{"unique_id":"578e7161f32df1.06784020","title":"boo"},{"unique_id":"578e0f7564c5c6.94428032","title":"lalalala"},{"unique_id":"578e0fbb8f95d8.48337399","title":"ghhhh"},{"unique_id":"578f5912ac55f4.71485786","title":"boo"},{"unique_id":"578f54c82c81c5.06156083","title":"jjjj"},{"unique_id":"578f53c251fb73.32146656","title":"ccc"},{"unique_id":"578f53c22ee9e2.44032439","title":"ccc"},{"unique_id":"578f533a759556.38700080","title":"ccc"},{"unique_id":"578f51beafcdd6.40560734","title":"hjjj"},{"unique_id":"578f51b3adc9c6.53224544","title":"hjjj"},{"unique_id":"578d82ebba7e82.61976731","title":"rhtyhrrh"},{"unique_id":"578d71ad826874.17496613","title":"fhghhh"},{"unique_id":"578d6d659605d7.58107965","title":"boo5003"},{"unique_id":"578d6d555f0774.18558468","title":"boo500000"},{"unique_id":"57933ca3e56379.52606857","title":"23072016-1"},{"unique_id":"57933cb6746b58.44233261","title":"23072016-2"},{"unique_id":"579356c1f37c94.92512350","title":"achallenge"},{"unique_id":"5793571d9238c4.62232112","title":"challenge"},{"unique_id":"5793571da78159.61806596","title":"challenge"},{"unique_id":"57935884e7bcf2.72928133","title":"achallenge"},{"unique_id":"579366336a78a5.36422777","title":"boooo"},{"unique_id":"579366dfe75647.32061097","title":"boooo"},{"unique_id":"57936f03c884b0.46671632","title":"fggggg"},{"unique_id":"57936f9be26a56.60274804","title":"boo"},{"unique_id":"57936fa0adf927.36072785","title":"boo"},{"unique_id":"57936fc695b686.27459827","title":"boo"},{"unique_id":"57937003032435.98039444","title":"boo"},{"unique_id":"579370f3b54f01.71840531","title":"boo"},{"unique_id":"5793785f5d6ff0.67364375","title":"booooo"},{"unique_id":"579379447425d8.12021062","title":"booooo"}]} 07-25 16:54:42.915 8134-8134/boo.famvsfam w/system.err: org.json.jsonexception: value connected of type java.lang.string cannot converted jsonobject 07-25 16:54:42.916 8134-8134/boo.famvsfam w/system.err: @ org.json.json.typemismatch(json.java:111) 07-25 16:54:42.916 8134-8134/boo.famvsfam w/system.err: @ org.json.jsonobject.<init>(jsonobject.java:160) 07-25 16:54:42.916 8134-8134/boo.famvsfam w/system.err: @ org.json.jsonobject.<init>(jsonobject.java:173) 07-25 16:54:42.916 8134-8134/boo.famvsfam w/system.err: @ boo.famvsfam.controller.challengelistactivity$4.onresponse(challengelistactivity.java:256) 07-25 16:54:42.916 8134-8134/boo.famvsfam w/system.err: @ boo.famvsfam.controller.challengelistactivity$4.onresponse(challengelistactivity.java:245) 07-25 16:54:42.916 8134-8134/boo.famvsfam w/system.err: @ com.android.volley.toolbox.stringrequest.deliverresponse(stringrequest.java:67) 07-25 16:54:42.916 8134-8134/boo.famvsfam w/system.err: @ com.android.volley.toolbox.stringrequest.deliverresponse(stringrequest.java:30) 07-25 16:54:42.916 8134-8134/boo.famvsfam w/system.err: @ com.android.volley.executordelivery$responsedeliveryrunnable.run(executordelivery.java:99) 07-25 16:54:42.916 8134-8134/boo.famvsfam w/system.err: @ android.os.handler.handlecallback(handler.java:739) 07-25 16:54:42.916 8134-8134/boo.famvsfam w/system.err: @ android.os.handler.dispatchmessage(handler.java:95) 07-25 16:54:42.916 8134-8134/boo.famvsfam w/system.err: @ android.os.looper.loop(looper.java:148) 07-25 16:54:42.916 8134-8134/boo.famvsfam w/system.err: @ android.app.activitythread.main(activitythread.java:5417) 07-25 16:54:42.916 8134-8134/boo.famvsfam w/system.err: @ java.lang.reflect.method.invoke(native method) 07-25 16:54:42.916 8134-8134/boo.famvsfam w/system.err: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) 07-25 16:54:42.916 8134-8134/boo.famvsfam w/system.err: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616)
i not think need post anymore code - problem seems j = new jsonobject(response);
after try. i'm new json problem has been around more day :( think ok after that, problem keep hitting no matter way try (and have tried plenty of tutorials , questions/answers) can please point out going wrong. first time tried json.
when instantiating jsonobject need pass actual string response:
ex: jsonobject obj = new jsonobject("result":[{"unique_id":"57933c641237d4.96974874","title":"23072016"},{"unique_id":"57901472464398.57585757","title":"hayley"}]");
given response.tostring()
gives string should following:
jsonobject j = new jsonobject(response.tostring());
p.s: think need if don't set content type in php :
<? header('content-type: application/json'); $servername = "localhost"; $username = "user"; $password = "pass"; $dbname ="android_api"; ....
Comments
Post a Comment