0

I have an AsyncTask that downloads a JSON string and parses the JSON:

private class GetHttpData extends AsyncTask<Void, Void, Void> {
    @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;
    }
}

makeHttpRequest() is:

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;
}

jsonToDB is:

private void jsonToDB(String json) throws JSONException,
        SQLiteException {
    try {
        JSONArray jArray = new JSONArray(json);
        int jLength = jArray.length();
        for (int i = 0; i < jLength; i++) {
            JSONObject category = jArray.getJSONObject(i);
            JSONArray dishes = category.getJSONArray(JSON_CATEGORY_DISHES);
            int dishesLength = dishes.length();
            for (int j = 0; j < 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;
    }
}

And menu_test.php prints this line:

[ {"catnumber":0,
   "catname":"Japanese",
   "dishes":[ { "number":"39",
                "name":"sushi",
                "content":"rice and fish",
                "price":6.99,
                "img":"imgurl"} ]
  } ]

Everything works just fine in Android 4.0 + but in 2.3 I get some strange behavior:

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

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:

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"}]}]

I can easily solve this by adding:

    while(!json.startsWith("[")) {
        json = json.substring(1);
    }

But still: Why is this hapenning? How do I solve this? Why is it only happenning in android 2.3 but not 4+?

VM4
  • 6,321
  • 5
  • 37
  • 51

1 Answers1

0

first, i think you didnt get the final data of content body .JSONObject / JSONArray .

you can try data = entity.getContent().toString(); this will not include any boundary of response , just JSON string .

evolever
  • 29
  • 2
  • 10
  • have you checked if there is any other illeagal charset out of [] ? maybe there is charset which can not be recognized from your server. – evolever Aug 02 '13 at 16:17