可能重复: JSON解析
我解析一个JSON文件(有效期)。它的工作原理在Android 4.0 - 4.0.4,但不是在旧的Android版本。 这是我的清单中的一部分:
I am parsing a JSON file (Which is valid). It works on Android 4.0 - 4.0.4 but not on older Android versions. This is a part of my Manifest:
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="14" />
这是我解析code:
And this is my parsing code:
public JSONObject getJSONFromUrl(String url) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
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 {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
和上了年纪的设备,我得到了以下错误消息(但正如我说的不是新的Android设备):
And on the older devices I get the following error message (But as I said not on new Android devices):
org.json.JSONException:java.lang.String类型的值不能转换为JSONObject的
org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
我完全不知道为什么它在Android 4,但不能在旧设备的工作。
I have absolutely no idea why it does work on Android 4 but not on older devices.
从这里 找到JSON的
Find the Json from here
这是可能的的JSONObject
解析器已经取得了新的Android版本较为宽松。您收到此错误信息出现是由于半信半疑法律JSON,特别是在接收端:
It is possible that the JSONObject
parser has been made more lenient in newer Android releases. The error message you are getting appears to be due to dubiously-legal JSON, particularly on the receiving side:
我会建议你写你下载的JSON到一个文件,并将其与原来的比较一下,看是否有与下载逻辑有问题。
I would suggest that you write your downloaded JSON out to a file and compare it with your original to see if there is a problem with the download logic.
更新
我无法重现你的问题。加载一个JSON关闭外部存储工作完全正常在Android 4.0.3,2.3.3,2.2和2.1,使用下面的活动。(注:我是懒惰和硬连接的路径外部存储):
I cannot reproduce your problem. Loading that JSON off of external storage works perfectly fine on Android 4.0.3., 2.3.3, 2.2, and 2.1, using the following activity (note: I was lazy and hard-wired in the path to external storage):
package com.commonsware.jsontest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.json.JSONException;
import org.json.JSONObject;
public class JSONTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
BufferedReader in=
new BufferedReader(new FileReader("/mnt/sdcard/test.json"));
String str;
StringBuilder buf=new StringBuilder();
while ((str=in.readLine()) != null) {
buf.append(str);
buf.append("\n");
}
in.close();
JSONObject json=new JSONObject(buf.toString());
((TextView)findViewById(R.id.stuff)).setText(json.toString());
}
catch (IOException e) {
Log.e(getClass().getSimpleName(), "Exception loading file", e);
}
catch (JSONException e) {
Log.e(getClass().getSimpleName(), "Exception parsing file", e);
}
}
}