我正在尝试从API提取数据。执行代码时,出现以下错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.smitsanghvi.moviedb, PID: 9844
java.lang.IndexOutOfBoundsException: Invalid index 7, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.smitsanghvi.moviedb.MainActivity$CheckConnectionStatus.onPostExecute(MainActivity.java:104)
at com.example.smitsanghvi.moviedb.MainActivity$CheckConnectionStatus.onPostExecute(MainActivity.java:44)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Application terminated.
MainActivity.java
下面的代码段。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new CheckConnectionStatus().execute("https://api.themoviedb.org/3/tv/popular?api_key=(IamNotProvidingInQuestion)&language=en-US&page=1");
}
//created an asynchronous task
class CheckConnectionStatus extends AsyncTask<String, Void, String> {
protected void onPreExecute(){
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
URL url = null;
try {
url = new URL(params[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream=urlConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
String s=bufferedReader.readLine();
bufferedReader.close();
return s;
} catch (IOException e) {
Log.e("Error:", e.getMessage(), e);
}
return null;
}
protected void onPostExecute(String s) {
super.onPostExecute(s);
//created json object to read and parse json
JSONObject jsonObject=null;
try {
jsonObject=new JSONObject(s);
//creating an arraylist for the tv season info
ArrayList<SeasonInfo>newlist=new ArrayList<>();
//reading objects from production companies as its an array
JSONArray jsonArray=jsonObject.getJSONArray("results");
//iterating to get the id from the company name
for(int i=0;i<jsonArray.length();i++)
{
JSONObject object=jsonArray.getJSONObject(i);
SeasonInfo seasonInfo=new SeasonInfo();
seasonInfo.setOriginal_name(object.getString("original_name"));
seasonInfo.setOverview(object.getString("overview"));
seasonInfo.setPoster_path(object.getString("poster_path"));
seasonInfo.setOriginal_language(object.getString("original_language"));
seasonInfo.setFirst_air_date(object.getString("first_air_date"));
}
Log.i("List:",newlist.get(0).getOverview());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
SeasonInfo.java
下面的代码段。
public class SeasonInfo {
public String original_name;
public String overview;
public String poster_path;
public String original_language;
public String first_air_date;
public String getPoster_path() {
return poster_path;
}
public void setPoster_path(String poster_path) {
this.poster_path = poster_path;
}
public String getOriginal_name() {
return original_name;
}
public void setOriginal_name(String original_name) {
this.original_name = original_name;
}
public String getOverview() {
return overview;
}
public void setOverview(String overview) {
this.overview = overview;
}
public String getOriginal_language() {
return original_language;
}
public void setOriginal_language(String original_language) {
this.original_language = original_language;
}
public String getFirst_air_date() {
return first_air_date;
}
public void setFirst_air_date(String first_air_date) {
this.first_air_date = first_air_date;
}
}
这是我正在我的应用程序中使用的来自网站的JSON数据。我在此处附加的JSON数据并不完整,只是第一部分,以便您可以更好地理解。
[{
"page": 1,
"total_results": 20024,
"total_pages": 1002,
"results": [
{
"original_name": "The Big Bang Theory",
"genre_ids": [],
"name": "The Big Bang Theory",
"popularity": 294.657,
"origin_country": [],
"vote_count": 3133,
"first_air_date": "2007-09-24",
"backdrop_path": "/nGsNruW3W27V6r4gkyc3iiEGsKR.jpg",
"original_language": "en",
"id": 1418,
"vote_average": 6.8,
"overview": "The Big Bang Theory is centered on five characters living in Pasadena, California: roommates Leonard Hofstadter and Sheldon Cooper; Penny, a waitress and aspiring actress who lives across the hall; and Leonard and Sheldon's equally geeky and socially awkward friends and co-workers, mechanical engineer Howard Wolowitz and astrophysicist Raj Koothrappali. The geekiness and intellect of the four guys is contrasted for comic effect with Penny's social skills and common sense.",
"poster_path": "/ooBGRQBdbGzBxAVfExiO8r7kloA.jpg"
}]
答案 0 :(得分:1)
尝试对这一行进行注释:
Log.i("List:",newlist.get(0).getOverview());
因为您从未向newlist
数组添加任何值,而您正试图获得一些值。
答案 1 :(得分:0)
您是否忘记在循环中添加newlist.add(seasonInfo);
?
否则,此循环将毫无意义,并且循环后的日志将引发异常。