我无法在我的应用中正确显示此内容。它告诉我它不能像这样解析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:Value { “系列”:0, “imdbid”: “tt3628580”, “流派”: “动画,喜剧”, “imdburl”: “http://www.imdb.com/title/tt3628580/”, “票”: “126”, “运行”: “21分钟”, “国”: “N / A”, “STV”:1, “语言”: “英语”, “称号”:“其 最有趣的人 世界”, “cacheExpiry”:1398972896, “年”: “132014”, “usascreens”:0, “评级”: “6.9”, “ukscreens”:0} 类型org.json.JSONObject无法转换为JSONArray
答案 0 :(得分:0)
为什么要在这些简单的步骤中完成所有这些操作。
第1步:建立连接并获取json。
public String loadJSON(String someURL) {
String json = null;
HttpClient mHttpClient = new DefaultHttpClient();
HttpGet mHttpGet = new HttpGet(someURL);
try {
HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
StatusLine statusline = mHttpResponse.getStatusLine();
int statuscode = statusline.getStatusCode();
if (statuscode != 200) {
return null;
}
InputStream jsonStream = mHttpResponse.getEntity().getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(jsonStream));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
json = builder.toString();
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
mHttpClient.getConnectionManager().shutdown();
return json;
}
下一步:使用该方法加载json,如下所示:
注意:您可以使用上述方法连接到任何网址并获取json。
public void ParseJSON(String URL){
try{
JSONObject mainJSONObject = new JSONObject(loadJSON(URL));
//If want just that particular block of information.
/*String getTitle = mainJSONObject.getString("title");
String Country = mainJSONObject.getString("country");*/
for(int i = 0;i<mainJSONObject.length();i++){//If you want all the data from the json.
String getTitle = mainJSONObject.getString("title");
String Country = mainJSONObject.getString("country");
//Rest you will figure it out.
}
}catch (Exception e){
e.printStackTrace();
}
}
下一步:使用AsyncTask在后台加载内容。
public class someTask extends AsyncTask<Void,Void,Void>{
ProgressDialog pDialog;
Context context;
String URL;
public someTask(Context context, String someURL){
super();
this.context = context;
this.URL = someURL;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Loading files...");
pDialog.setIndeterminate(true);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
ParseJSON(URL);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
pDialog.dismiss();
}
}
最后一步:只需执行onCreate()中的任务,就像这样:
new someTask(context,your_url_you_want_to_execute).execute();
希望这个解决方案很简单,并帮助您解决问题。 Lemme知道它有效。祝你好运.. :)