【发布时间】:2014-04-24 21:24:31
【问题描述】:
我无法让它在我的应用程序中正确显示。它告诉我它不能像这样解析 JSON。这是执行此操作的代码:
private class SearchActivity2 extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... voids) {
String result = "";
String s = "";
InputStream isr = null;
HttpClient httpclient = null;
try {
httpclient = new DefaultHttpClient();
} catch (Exception ex) {
//show.setText(ex.getMessage());
System.exit(1);
}
HttpGet httpget = null;
try {
httpget = new HttpGet("http://deanclatworthy.com/imdb/?q=" + search);
//System.out.println("http://deanclatworthy.com/imdb/?q=" + search);
} catch (IllegalArgumentException ex) {
//show.setText(ex.getMessage());
System.exit(1);
}
HttpResponse response = null;
try {
response = httpclient.execute(httpget);
} catch (IOException ex) {
//show.setText(ex.getMessage());
System.exit(1);
}
try {
HttpEntity entity = response.getEntity();
isr = entity.getContent();
} catch (IOException ex) {
//show.setText(ex.getMessage());
System.exit(1);
} catch (IllegalStateException ex) {
//show.setText(ex.getMessage());
System.exit(1);
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(isr, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
//parse json data
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json = jArray.getJSONObject(i);
s = s +
"Title : " + json.getString("title") + "\n" +
"Rating : " + json.getString("rating") + "\n";
}
//show.setText(s);
return s;
} catch (Exception e) {
//TODO: handle exception
Log.e("log_tag", "Error Parsing Data " + e.toString());
}
return s;
}
protected void onPostExecute(String s){
showSearch.setText(s);
}
}
目前,我只是尝试显示该网站的标题和评级。在这种情况下,无论用户输入什么,它都会是 http://deanclatworthy.com/imdb/?q= +。所以,http://deanclatworthy.com/imdb/?q=The+Incredible+Hulk 会起作用,或者类似的东西。
这里是 logcat 错误:04-24 14:34:56.009 11886-12595/com.android.movies E/log_tag:
解析数据时出错 org.json.JSONException: 值 {“系列”:0,“imdbid”:“tt3628580”,“流派”:“动画,喜剧”,“imdburl”:“http://www.imdb.com/title/tt3628580/”,“投票”: "126","runtime":"21min","country":"n/a","stv":1,"languages":"English","title":"The 世界上最有趣的人 World","cacheExpiry":1398972896,"year":"132014","usascreens":0,"rating":"6.9","ukscreens":0} org.json.JSONObject 类型的无法转换为 JSONArray
【问题讨论】:
-
我认为结果根本不是一个数组......也许是因为在我尝试使用 json 之前我读过它......