JSON 数据被视为 XML,如何解决?
JSON data seen as XML, how to fix?
我正在尝试显示来自此 link 的 JSON 数据。
我不断收到的错误是:java.lang.string 类型的值 xml 无法转换为 JSONObject。但我很确定 link 显示 JSON。有什么建议吗?
这是我目前拥有的:
public class MainActivity extends Activity {
TextView txt1;
TextView txt2;
//TextView txt3;
private static String url = "http://opendata.rijksoverheid.nl/v1/sources/rijksoverheid/infotypes/schoolholidays/schoolyear/2015-2016?output=json";
private static final String TAG_CONTENT = "content";
private static final String TAG_TITLE = "title";
private static final String TAG_SCHOOLYEAR = "schoolyear";
//private static final String TAG_EMAIL = "email";
JSONArray content = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new JSONParse().execute();
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
protected void onPreExecute() {
super.onPreExecute();
txt1 = (TextView)findViewById(R.id.textView1);
txt2 = (TextView)findViewById(R.id.textView2);
//txt3 = (TextView)findViewById(R.id.textView3);
}
@Override
protected JSONObject doInBackground(String... arg0) {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
protected void onPostExecute(JSONObject json) {
try {
content = json.getJSONArray(TAG_CONTENT);
JSONObject c = content.getJSONObject(0);
String title = c.getString(TAG_TITLE);
String schoolyear = c.getString(TAG_SCHOOLYEAR);
//String email = c.getString(TAG_EMAIL);
txt1.setText(title);
txt2.setText(schoolyear);
//txt3.setText(email);
} catch(JSONException e) {
e.printStackTrace();
}
}
}
}
public class JSONParser {
static InputStream is = null;
static JSONObject jobj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// HTTP request
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("Error", "Error converting result" + e.toString());
}
// parse string to JSON object
try {
jobj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data" + e.toString());
}
// return JSON string
return jobj;
}
}
就加载 JSON 文件而言,您可以使用异步任务并下载 JSON 文件。
既然你提到了术语 largescale
,我假设你在阅读 JSON 文件时正在寻找一些快速的性能。
您可以尝试一些 JSON 解析库,而不是使用本机包 (org.json)。
希望对您有所帮助。
显然将这一行中的 HttpPost 更改为 HttpGet 就成功了。不过不知道为什么。
HttpPost httpPost = new HttpPost(url);
to
HttpGet httpPost = new HttpGet(url);
我正在尝试显示来自此 link 的 JSON 数据。
我不断收到的错误是:java.lang.string 类型的值 xml 无法转换为 JSONObject。但我很确定 link 显示 JSON。有什么建议吗?
这是我目前拥有的:
public class MainActivity extends Activity {
TextView txt1;
TextView txt2;
//TextView txt3;
private static String url = "http://opendata.rijksoverheid.nl/v1/sources/rijksoverheid/infotypes/schoolholidays/schoolyear/2015-2016?output=json";
private static final String TAG_CONTENT = "content";
private static final String TAG_TITLE = "title";
private static final String TAG_SCHOOLYEAR = "schoolyear";
//private static final String TAG_EMAIL = "email";
JSONArray content = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new JSONParse().execute();
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
protected void onPreExecute() {
super.onPreExecute();
txt1 = (TextView)findViewById(R.id.textView1);
txt2 = (TextView)findViewById(R.id.textView2);
//txt3 = (TextView)findViewById(R.id.textView3);
}
@Override
protected JSONObject doInBackground(String... arg0) {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
protected void onPostExecute(JSONObject json) {
try {
content = json.getJSONArray(TAG_CONTENT);
JSONObject c = content.getJSONObject(0);
String title = c.getString(TAG_TITLE);
String schoolyear = c.getString(TAG_SCHOOLYEAR);
//String email = c.getString(TAG_EMAIL);
txt1.setText(title);
txt2.setText(schoolyear);
//txt3.setText(email);
} catch(JSONException e) {
e.printStackTrace();
}
}
}
}
public class JSONParser {
static InputStream is = null;
static JSONObject jobj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// HTTP request
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("Error", "Error converting result" + e.toString());
}
// parse string to JSON object
try {
jobj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data" + e.toString());
}
// return JSON string
return jobj;
}
}
就加载 JSON 文件而言,您可以使用异步任务并下载 JSON 文件。
既然你提到了术语 largescale
,我假设你在阅读 JSON 文件时正在寻找一些快速的性能。
您可以尝试一些 JSON 解析库,而不是使用本机包 (org.json)。
希望对您有所帮助。
显然将这一行中的 HttpPost 更改为 HttpGet 就成功了。不过不知道为什么。
HttpPost httpPost = new HttpPost(url);
to
HttpGet httpPost = new HttpGet(url);