【问题标题】:Parsing JSON in Android 2.3 fails after getting it from server从服务器获取 JSON 后在 Android 2.3 中解析 JSON 失败
【发布时间】:2013-08-02 15:30:07
【问题描述】:

我有一个 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+?

【问题讨论】:

    标签: android json android-version


    【解决方案1】:

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

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

    【讨论】:

    • 您是否检查过 [] 中是否还有其他非法字符集?可能有您的服务器无法识别的字符集。
    猜你喜欢
    • 2017-10-04
    • 2015-10-19
    • 2015-01-19
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多