Note that there are some explanatory texts on larger screens.

plurals
  1. POget JSONException: Value of type java.lang.String cannot be converted to JSONObject when parsing a JSON response
    primarykey
    data
    text
    <p>I have developed an Android application that requests places coordinates from a server which responds in JSON format(for the moment it just sends two places):</p> <p>this is the php code from the server: </p> <pre><code>$place = $db-&gt;getCoordinates($name); if ($place != false) { $response[1]["success"] = 1; $response[1]["place"]["H"] = $place[1]["H"]; $response[1]["place"]["V"] = $place[1]["V"]; $response[1]["place"]["placeid"] = $place[1]["placeid"]; $response[1]["place"]["name"] = $place[1]["name"]; $response[1]["place"]["type"] = $place[1]["type"]; $response[1]["place"]["note"] = $place[1]["note"]; // place found // echo json with success = 1 $response[2]["success"] = 1; $response[2]["place"]["H"] = $place[2]["H"]; $response[2]["place"]["V"] = $place[2]["V"]; $response[2]["place"]["placeid"] = $place[2]["placeid"]; $response[2]["place"]["name"] = $place[2]["name"]; $response[2]["place"]["type"] = $place[2]["type"]; $response[2]["place"]["note"] = $place[2]["note"]; echo json_encode($response); } </code></pre> <p>When the app get the coordinates it try to parse them this way:</p> <pre><code> JSONObject json_places = userFunction.getPlaces(); JSONObject places = json_places.getJSONObject("1"); JSONObject coord = places.getJSONObject("place"); </code></pre> <p>getplaces():</p> <pre><code>public JSONObject getPlaces(){ // Building Parameters List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;(); params.add(new BasicNameValuePair("tag", allcoordinates_tag)); JSONObject json = jsonParser.getJSONFromUrl("http://"+ip+placesURL, params); // return json Log.i("JSON", json.toString()); return json; } </code></pre> <p>JSONParser:</p> <pre><code>public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } public JSONObject getJSONFromUrl(String url, List&lt;NameValuePair&gt; params) { // Making HTTP request try { // defaultHttpClient HttpPost httpPost = new HttpPost(url); HttpParams httpParameters = new BasicHttpParams(); int timeoutConnection = 9000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); int timeoutSocket = 9000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ConnectTimeoutException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (Exception 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(); Log.e("JSON", json); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // try parse the string to a JSON object try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // return JSON String return jObj; } } </code></pre> <p>and here is the JSON with the error:</p> <p><img src="https://i.stack.imgur.com/G4REF.jpg" alt="debug from eclipse"></p> <p>when the App tries to parse the JSON response it crashes and sends a JSONException: Value of type java.lang.String cannot be converted to JSONObject how can i solve this? THANKS</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload