0

我有一个 AsyncTask 下载 JSON 字符串并解析 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() 是:

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 是:

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

menu_test.php 打印出这一行:

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

在 Android 4.0 + 中一切正常,但在 2.3 中我得到一些奇怪的行为:

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

一个符号被添加到我什至无法复制的前面。如果我从 makeHttpRequest() 中的第 1 行中删除 UTF-8 编码,则会在前面添加更多的 jiberish:

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

我可以通过添加以下内容轻松解决此问题:

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

但仍然: 为什么会发生这种情况?我该如何解决这个问题?为什么它只发生在 android 2.3 而不是 4+?

4

1 回答 1

0

首先,我认为您没有得到内容主体 .JSONObject / JSONArray 的最终数据。

你可以试试 data = entity.getContent().toString();这将不包括任何响应边界,只是 JSON 字符串。

于 2013-08-02T15:43:52.733 回答