php - Error parsing data org.json.JSONException: type java.lang.String cannot be converted to JSONObject -
currently php file used interface between mysql server , android app looks like:
<?php // array json response $response = array(); if (isset($_get['porfileid'])) { $id = $_get['porfileid']; // include db connect class require_once __dir__ . '/db_connect.php'; // connecting db $db = new db_connect(); // products products table $result = mysql_query("select * profiles_profilelists profileid = '$id'") or die(mysql_error()); // check empty result if (mysql_num_rows($result) > 0) { // looping through results // products node $response["lists"] = array(); while ($row = mysql_fetch_array($result)) { // temp user array $lists = array(); $lists["profilelistid"] = $row["profilelistid"]; $lists["profilelistname"] = $row["profilelistname"]; // push single product final response array array_push($response["lists"], $lists); } // success $response["success"] = 1; // echoing json response echo json_encode($response); } else { // no products found $response["success"] = 0; $response["message"] = "no products found"; // echo no users json echo json_encode($response); } } else { // required field missing $response["success"] = 0; $response["message"] = "required field(s) missing"; // echoing json response echo json_encode($response); } ?>
this works fine when add:
$result2 = mysql_query("select * profiles_profilelistitems profilelistid = ".$row["profilelistname"]) or die(mysql_error()); $lists["listcount"] = mysql_num_rows($result2);
to php looks like:
<?php // array json response $response = array(); if (isset($_get['porfileid'])) { $id = $_get['porfileid']; // include db connect class require_once __dir__ . '/db_connect.php'; // connecting db $db = new db_connect(); // products products table $result = mysql_query("select * profiles_profilelists profileid = '$id'") or die(mysql_error()); // check empty result if (mysql_num_rows($result) > 0) { // looping through results // products node $response["lists"] = array(); while ($row = mysql_fetch_array($result)) { // temp user array $lists = array(); $lists["profilelistid"] = $row["profilelistid"]; $lists["profilelistname"] = $row["profilelistname"]; $result2 = mysql_query("select * profiles_profilelistitems profilelistid = ".$row["profilelistname"]) or die(mysql_error()); $lists["listcount"] = mysql_num_rows($result2); // push single product final response array array_push($response["lists"], $lists); } // success $response["success"] = 1; // echoing json response echo json_encode($response); } else { // no products found $response["success"] = 0; $response["message"] = "no products found"; // echo no users json echo json_encode($response); } } else { // required field missing $response["success"] = 0; $response["message"] = "required field(s) missing"; // echoing json response echo json_encode($response); } ?>
i end above error. complete trace is:
09-03 17:05:57.836: e/json parser(675): error parsing data org.json.jsonexception: value of type java.lang.string cannot converted jsonobject 09-03 17:05:59.617: e/androidruntime(675): fatal exception: asynctask #1 09-03 17:05:59.617: e/androidruntime(675): java.lang.runtimeexception: error occured while executing doinbackground() 09-03 17:05:59.617: e/androidruntime(675): @ android.os.asynctask$3.done(asynctask.java:299) 09-03 17:05:59.617: e/androidruntime(675): @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:273) 09-03 17:05:59.617: e/androidruntime(675): @ java.util.concurrent.futuretask.setexception(futuretask.java:124) 09-03 17:05:59.617: e/androidruntime(675): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:307) 09-03 17:05:59.617: e/androidruntime(675): @ java.util.concurrent.futuretask.run(futuretask.java:137) 09-03 17:05:59.617: e/androidruntime(675): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 09-03 17:05:59.617: e/androidruntime(675): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) 09-03 17:05:59.617: e/androidruntime(675): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) 09-03 17:05:59.617: e/androidruntime(675): @ java.lang.thread.run(thread.java:856) 09-03 17:05:59.617: e/androidruntime(675): caused by: java.lang.nullpointerexception 09-03 17:05:59.617: e/androidruntime(675): @ com.test.app.mylists$loadalllists.doinbackground(mylists.java:267) 09-03 17:05:59.617: e/androidruntime(675): @ com.test.app.mylists$loadalllists.doinbackground(mylists.java:1) 09-03 17:05:59.617: e/androidruntime(675): @ android.os.asynctask$2.call(asynctask.java:287) 09-03 17:05:59.617: e/androidruntime(675): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) 09-03 17:05:59.617: e/androidruntime(675): ... 5 more
the code trace links within app:
protected string doinbackground(string... args) { // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("porfileid", integer.tostring(userid))); // getting json string url jsonobject json = jparser.makehttprequest(url_all_products, "get", params); // check log cat json reponse log.d("all products: ", json.tostring()); try { // checking success tag int success = json.getint(tag_success); if (success == 1) { // products found // getting array of products products = json.getjsonarray("lists"); productslist.clear(); // looping through products (int = 0; < products.length(); i++) { jsonobject c = products.getjsonobject(i); // storing each json item in variable string id = c.getstring("profilelistid"); string name = c.getstring("profilelistname"); //string count = c.getstring("listcount"); // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); // adding each child node hashmap key => value map.put("profilelistid", id); map.put("profilelistname", name); // adding hashlist arraylist productslist.add(map); } } else { } } catch (jsonexception e) { e.printstacktrace(); } return null; }
and json parser using
public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } // function json url // making http post or method public jsonobject makehttprequest(string url, string method, list<namevaluepair> params) { // making http request try { // check request method if(method == "post"){ // request method post // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params)); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); }else if(method == "get"){ // request method defaulthttpclient httpclient = new defaulthttpclient(); string paramstring = urlencodedutils.format(params, "utf-8"); url += "?" + paramstring; httpget httpget = new httpget(url); httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); json = sb.tostring(); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json string return jobj; } }
if getting error after introducing php line may have error in query itself. try dumping query more insights.
select * profiles_profilelistitems profilelistid = ".$row["profilelistname"]
should be
select * profiles_profilelistitems profilelistid = '".$row["profilelistname"]."'"
Comments
Post a Comment