Json在Android ICS上工作得很好但不适用于2.3

时间:2013-02-02 12:11:54

标签: android json android-4.0-ice-cream-sandwich android-2.3-gingerbread

        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 + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }    
// try parse the string to a JSON object
    try {
         // convert string to jsonObject
        Log.d("JSON Parser", "json: "+json);
        jObj = new JSONObject(json);

    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

Log.d("JSON Parser", "json: "+json);作为输出(logcat):

02-02 12:08:31.371: D/JSON Parser(407): json: {"code":64,"message":"Segnalazione avvenuta con successo"}

但它会进入捕获区并在logcat中读到:

02-02 12:08:31.371: E/JSON Parser(407): Error parsing data org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject

这项工作在Android ICS中非常不错,但在Android 2.3中无法使用

编辑:我用

解决
if (result.startsWith("["))
  {



  }
  else
  {
      result= result.substring(1);
  }

但我不认为这是一个正确的策略。

1 个答案:

答案 0 :(得分:0)

您是否尝试将responseType设置为json。如果问题仍然存在,这些帖子中有一些建议  JSON Parsing works on Android 4.0 but not on Android < 4.0
 Issue initializing a JSONObject
 JSON parsing problem