내 응용 프로그램에서 올바르게 표시되도록하는 데 문제가 있습니다. JSON을 이와 같이 구문 분석 할 수 없다고 알려줍니다. 다음은이를 수행하는 코드입니다.URL에서 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이 작동합니다. 04-24 14 : 34 : 56.009 11886-12595/com.android.movies E/log_tag : 당신이 그것을 할 만 할 수있을 때
Error Parsing Data org.json.JSONException: Value {"series":0,"imdbid":"tt3628580","genres":"Animation,Comedy","imdburl":"http://www.imdb.com/title/tt3628580/","votes":"126","runtime":"21min","country":"n/a","stv":1,"languages":"English","title":"The Most Interesting Man in the World","cacheExpiry":1398972896,"year":"132014","usascreens":0,"rating":"6.9","ukscreens":0} of type org.json.JSONObject cannot be converted to JSONArray
나는 그 결과가 배열이 아니라고 생각합니다 ... 아마 내가 전에 json을 사용하려고했기 때문에 ... 그것에 대해 읽었습니다 ... – Selvin