Note that there are some explanatory texts on larger screens.

plurals
  1. POJSONObject parsed on android 4.0, but not on earlier versions
    primarykey
    data
    text
    <p>I have this code which fetches a json string from a website:</p> <pre><code>post.setEntity(entity); HttpResponse responsePOST = client.execute(post); HttpEntity resEntity = responsePOST.getEntity(); result = EntityUtils.toString(resEntity, HTTP.UTF_8); </code></pre> <p>This is an example of the json in result:</p> <pre><code>{"status":"0","user_id":"123","name":"test","session_code":"6f33d4ee610651530f04b1f700ebc36d"} </code></pre> <p>which validates on jsonlint.com. I try to parse it using</p> <pre><code>JSONObject jo = new JSONObject(result); </code></pre> <p>On android 4, the code works fine, but on earlier versions, it gives</p> <pre><code>org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject </code></pre> <p>When I hard coded the json string instead of getting it online, it worked on earlier versions. When I logged the http request output I found that it's identical in every thing except for the session code which changes with every successful request but both session codes had the same length and format.</p> <p>Any one knows what is the reason for this and how to fix it?</p> <hr> <p>UPDATE:</p> <pre><code>if (!email.isEmpty() &amp;&amp; !password.isEmpty()){ try { HttpClient client = new DefaultHttpClient(); String postURL = "http://www.xxxxxx.com/api/login.php"; HttpPost post = new HttpPost(postURL); List&lt;NameValuePair&gt; postParams = new ArrayList&lt;NameValuePair&gt;(); postParams.add(new BasicNameValuePair("email", email)); postParams.add(new BasicNameValuePair("pass", password)); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams, HTTP.UTF_8); post.setEntity(entity); HttpResponse responsePOST = client.execute(post); HttpEntity resEntity = responsePOST.getEntity(); result = EntityUtils.toString(resEntity, HTTP.UTF_8); } catch (UnsupportedEncodingException e) { Log.e(TAG, e.toString()); } catch (ClientProtocolException e) { Log.e(TAG, e.toString()); } catch (ParseException e) { Log.e(TAG, e.toString()); } catch (IOException e) { Log.e(TAG, e.toString()); } } if (result != null) { try { JSONObject jo = new JSONObject(result); switch (jo.getInt("status")) { case 0 : { userName = jo.getString("name"); userID = jo.getInt("user_id"); authCode = jo.getString("session_code"); return 0; } default: return jo.getInt("status"); } } catch (JSONException e) { Log.e(TAG, e.toString()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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