Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing JSON in Android 2.3 fails after getting it from server
    text
    copied!<p>I have an AsyncTask that downloads a JSON string and parses the JSON:</p> <pre><code>private class GetHttpData extends AsyncTask&lt;Void, Void, Void&gt; { @Override protected Void doInBackground(Void... params) { String result = null; try { result = makeHttpRequest(HOST + "/menu_test.php"); Log.v(TAG, result); jsonToDB(result); } catch (Exception e) { Log.e(TAG, "", e); } return null; } } </code></pre> <p>makeHttpRequest() is:</p> <pre><code>public String makeHttpRequest(String Url) throws HttpException, IOException { String data = null; HttpPost post = new HttpPost(Url); HttpParams httpParams = new BasicHttpParams(); // setting some params ... HttpClient client = new DefaultHttpClient(httpParams); try { HttpResponse response = client.execute(post); int status = response.getStatusLine().getStatusCode(); if (status == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); data = EntityUtils.toString(entity, "UTF-8"); // #1 } else { throw new HttpException(); } } catch (HttpException e) { throw e; } catch (ConnectTimeoutException e) { throw e; } catch (ClientProtocolException e) { throw e; } catch (IOException e) { throw e; } finally { client.getConnectionManager().shutdown(); } return data; } </code></pre> <p>jsonToDB is:</p> <pre><code>private void jsonToDB(String json) throws JSONException, SQLiteException { try { JSONArray jArray = new JSONArray(json); int jLength = jArray.length(); for (int i = 0; i &lt; jLength; i++) { JSONObject category = jArray.getJSONObject(i); JSONArray dishes = category.getJSONArray(JSON_CATEGORY_DISHES); int dishesLength = dishes.length(); for (int j = 0; j &lt; dishesLength; j++) { JSONObject dish = dishes.getJSONObject(j); Log.v(TAG, dish.getString(JSON_DISH_NUMBER)); } } } catch (JSONException e) { Log.e(TAG, "Error parsing JSON data" + e.toString()); throw e; } } </code></pre> <p>And menu_test.php prints this line:</p> <pre><code>[ {"catnumber":0, "catname":"Japanese", "dishes":[ { "number":"39", "name":"sushi", "content":"rice and fish", "price":6.99, "img":"imgurl"} ] } ] </code></pre> <p>Everything works just fine in Android 4.0 + but in 2.3 I get some strange behavior:</p> <pre><code>08-02 15:24:22.675: V/test(947): ?[{"catnumber":0,"catname":"Japanese","dishes": [{"number":"39","name":"sushi","content":"rice and fish","price":6.99,"img":"imgurl"}]}] 08-02 15:24:22.675: E/test(947): Error parsing JSON dataorg.json.JSONException: Value ? of type java.lang.String cannot be converted to JSONArray </code></pre> <p>A symbol gets added to the front that I can't even copy. If I remove the UTF-8 encoding from Line #1 in makeHttpRequest() even more jiberish gets added to the front:</p> <pre><code>08-02 15:27:58.914: V/test(981): [{"catnumber":0,"catname":"Japanese","dishes":[{"number":"39","name":"sushi","content":"rice and fish","price":6.99,"img":"imgurl"}]}] </code></pre> <p>I can easily solve this by adding:</p> <pre><code> while(!json.startsWith("[")) { json = json.substring(1); } </code></pre> <p>But still: <strong>Why is this hapenning? How do I solve this? Why is it only happenning in android 2.3 but not 4+?</strong></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