这个return这里要返回啥?
package com.imooc.loadermovie;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.AsyncTaskLoader;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MoveAsyncTaskLoader extends AsyncTaskLoader<List<MovieBean>> {
private static final String TAG = "MovieLoader";
public MoveAsyncTaskLoader(Context context) {
super(context);
}
//如果loader启动了,强制执行loadInBackgroun()
@Override
protected void onStartLoading() {
super.onStartLoading();
if(isStarted()){
forceLoad();
}
}
@Override
public List<MovieBean> loadInBackground() {
Log.d(TAG,"开始请求数据");
//List<MovieBean> movieBeans = new ArrayList<MovieBean>();
StringBuffer stringBuffer = new StringBuffer();
String result = null;
MovieBean movieBean = null;
try {
URL url = new URL("http://www.imooc.com/api/movie");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5*1000);
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
String strRead = null;
while((strRead=reader.readLine())!=null){
stringBuffer.append(strRead);
}
reader.close();
result = stringBuffer.toString();
inputStream.close();
try {
movieBean = new MovieBean();
JSONObject jsonObject = new JSONObject(result);
List<MovieLess> lessList = new ArrayList<>();
int count = jsonObject.getInt("count");
int start = jsonObject.getInt("start");
int total = jsonObject.getInt("total");
JSONArray lessons = jsonObject.getJSONArray("movies");
movieBean.setCount(count);
movieBean.setStart(start);
movieBean.setTotal(total);
if(lessons !=null &&lessons.length()>0){
for(int index = 0;index<lessons.length();index++){
JSONObject lesson = (JSONObject)lessons.get(index);
int id=lesson.getInt("id");
List<String> rating = (List<String>) lesson.getJSONObject("rating");
List<String> types = (List<String>) lesson.getJSONArray("types");
String title = lesson.getString("title");
String description = lesson.getString("description");
List<String> casts = (List<String>) lesson.getJSONArray("casts");
List<String> directors = (List<String>)lesson.getJSONArray("directors");
String year = lesson.getString("year");
String imageUrl = lesson.getString("imageUrl");
//赋值
MovieLess movieLess = new MovieLess();
movieLess.setId(id);
movieLess.setRating(rating);
movieLess.setTypes(types);
movieLess.setTitle(title);
movieLess.setDescription(description);
movieLess.setCasts(casts);
movieLess.setDirectors(directors);
movieLess.setYear(year);
movieLess.setImageUrl(imageUrl);
lessList.add(movieLess);
}
movieBean.setMovies(lessList);
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return movieBean;
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 147 份
- 解答问题 687 个
本专题介绍了Android开发核心组件:广播、服务及全局应用。教会你如何使用AIDL、Thread、Socket、蓝牙解决进程线程间通信问题。利用Glide等实现异步加载及NDK原生代码开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星