Note that there are some explanatory texts on larger screens.

plurals
  1. POException parsing string to JSONObject (works at local server, but not at the online server) android
    text
    copied!<p>the code:</p> <pre><code>public JSONArray getJSONFromUrl(String url, List&lt;NameValuePair&gt; params) { // Making HTTP request try { // defaultHttpClient HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = 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, "UTF-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line); } 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 { JSONObject json_data = new JSONObject(json); JSONArray hashMap_names = json_data.names(); JSONArray hashMap_names2 = new JSONArray(); Map hashMap = new HashMap(json_data.length()); for (int i=0; i!=hashMap_names.length(); i++){ //Object obj = chaves.next(); hashMap.put(String.valueOf(i),json_data.get(String.valueOf(i))); hashMap_names2.put(String.valueOf(i)); } JSONObject hashMap_obj = new JSONObject (hashMap); jArr = hashMap_obj.toJSONArray(hashMap_names2); Log.e("JSON Parser", "succesful parsing data " + jArr.toString()); } catch (Exception e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); jArr = null; } return jArr; } </code></pre> <p>after the stringbuilder.toString(), json has the folowwing value: </p> <pre><code>{"0":"27124","1":"Adsad adadda daddadadad ","2":"asdasdas@gmail.com","3":"732bcv874uhfebfehuif9724uhife","4":"wasd","5":"","6":"M","7":"","8":"","9":"","10":"","11":"","12":"06\/05\/1989","13":"","14":"","15":"","16":"","17":"","18":"","19":"","20":"BR","21":"","22":"0","23":"","24":"","25":"","26":"Y","27":"Y","28":"Y","29":"N","30":"0","31":"30\/04\/2012 16:48:20","32":"17\/04\/2012 01:09:27","33":"367","34":"50","35":"0","36":"79","37":"34","38":"","39":"17\/04\/2012 01:16:54","40":"3649","41":[null,null,null,null,null,null,null,null,null,null,null]} </code></pre> <p>wich looks to me a perfect formated JSON text.</p> <p>but when I try creating a new JSONObject(json), get the exception </p> <pre><code>Error parsing data org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray </code></pre> <p>BUT I only get this error when using the online server. If I use the local one (xammp), json is parsed to JSONObject and the app works.</p> <p>I've tried to set <code>json = "{'0':'1212','1':'username','2':'email','3':'pass'}";</code> and it worked! but when using <code>json = "\""+json.replace('\"', '\'')+"\"";</code> just got the same exception</p> <p>by the way, i'm using the hashmap just to ordenate de JSONObject after parsing.</p> <p>Maybe the issue its because on local I'm using php 5.3, and the online server uses php 5.2? There is any difference between the headers of these versions? How could I verificate this?</p>
 

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