我的应用程序在从API获取所有信息之后崩溃,不知道为什么,我猜它是文本更改的东西,但正如我所说,不知道,这是我第一次使用API,所以这里是代码:
之前有线,我试图解决这个问题,但是我坚持下去,所以我很乐意得到一些帮助:)
jsonString = sb.toString();
JSONObject jObject = new JSONObject(jsonString);
JSONObject jObject2 = jObject.getJSONObject("data");
Log.v("data =>",jObject2.toString());
comicsNameOutput(jObject2);
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
//provide the "result" section
protected void comicsNameOutput(JSONObject jObject2) {
try{
JSONArray comicInfoObject = jObject2.getJSONArray("results");
Log.v("results =>",comicInfoObject.toString());
finalDataProvidor(comicInfoObject);
}catch (JSONException e){
e.printStackTrace();
}
}
//get final data about each comic book
protected void finalDataProvidor(JSONArray comicInfoObject) {
String[] dataAboutEachComic = {"id", "title", "issueNumber", "pageCount"};
try{
for(int i = 0; i < comicInfoObject.length(); i++){
JSONObject dataPerComicObject = comicInfoObject.getJSONObject(i);
result = result + dataAboutEachComic[i] + " : " + dataPerComicObject.getString(dataAboutEachComic[i]) + "\n";
Log.v("finalResult =>",result.toString());
}
}catch (JSONException e){
e.printStackTrace();
}
}
@Override
protected void onPostExecute(Void aVoid){
TextView textView1 = (TextView) findViewById(R.id.textView1);
textView1.setText(this.result);
}
}
logcat查看我请求的信息,但正如我所说,它从API获取所有信息后崩溃了。 logcat上的错误:
java.lang.ArrayIndexOfBoundsExecption:legnth = 4; index = 4
线程退出与未捕获的execption
答案 0 :(得分:0)
我对阵列有问题,我错误地使用了错误的阵列(使用了我,这比我需要的更大。
for(int i = 0; i < comicInfoObject.length(); i++){
JSONObject dataPerComicObject = comicInfoObject.getJSONObject(i);
for(int k = 0; k < dataAboutEachComic.length; k++){
result = result + dataAboutEachComic[k] + " : " + dataPerComicObject.getString(dataAboutEachComic[k]) + "\n";
}