OKHTTP
public class OKHttpHelper {
private static final String TAG = "OKHttp";
private static final String BASE_URL = "http://api.douban.com";
private static final String MOVIE_URL = "/v2/movie/top250";
private static final String SUB_URL = "/v2/movie/subject/";
private static final String API_KEY = "?apikey=0b2bdeda43b5688921839c8ecb20399b";
private Context mContext;
private int mCount = 0;
private int mTotal = 0;
private HashMap<String, Integer> mMovieMap = new HashMap<>();
private ArrayList<ImgLoader> mImgList = new ArrayList<>();
private CountDownLatch mCountLatch;
private OkHttpClient client = new OkHttpClient();
public OKHttpHelper(Context context) {
mContext = context;
}
public void request() {
final Request request = new Request.Builder().url(BASE_URL + MOVIE_URL + API_KEY).build();
client.newCall(request).enqueue(new MainCallback());
}
public void request(int id) {
final Request request = new Request.Builder().url(BASE_URL + SUB_URL + id + API_KEY).build();
client.newCall(request).enqueue(new MainCallback());
}
private void loadImg(String title, String url) {
final Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(new ImgCallback(title));
}
private void parseJson(String content) throws JSONException {
JSONObject jsonObject = new JSONObject(content);
String count = jsonObject.getString("count");
String total = jsonObject.getString("total");
JSONArray subArray = jsonObject.getJSONArray("subjects");
for (int j = 0; j < subArray.length(); j++) {
JSONObject subjects = subArray.getJSONObject(j);
String title = subjects.getString("title");
String idStr = subjects.getString("id");
JSONObject images = subjects.getJSONObject("images");
String imgUrl = images.getString("large");
if (idStr != null) {
int id = Integer.parseInt(idStr);
mMovieMap.put(title, id);
}
if (imgUrl != null) {
loadImg(title, imgUrl);
}
}
if (count != null) {
mCount = Integer.parseInt(count);
mCountLatch = new CountDownLatch(mCount);
new Thread(new WaitJob()).start();
}
if (total != null) {
mTotal = Integer.parseInt(total);
}
Log.d(TAG, "parseJson " + mTotal + " " + mCount);
mMovieMap.entrySet().forEach(e->Log.d(TAG, "title " + e.getKey() + " id " + e.getValue()));
}
private class MainCallback implements Callback {
@Override
public void onFailure(Request request, IOException e) {
Log.d(TAG, "onFailure");
}
@Override
public void onResponse(Response response) throws IOException {
String content = response.body().string();
Log.d(TAG, "onResponse " + content);
try {
parseJson(content);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private class WaitJob implements Runnable {
@Override
public void run() {
Log.d(TAG, "waiting for count down");
if (mCountLatch != null) {
try {
mCountLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Intent intent = new Intent(mContext, MovieActivity.class);
intent.putParcelableArrayListExtra("data", mImgList);
mContext.startActivity(intent);
}
}
private class ImgCallback implements Callback {
private String title;
public ImgCallback(String name) {
title = name;
}
@Override
public void onFailure(Request request, IOException e) {
Log.d(TAG, "onFailure");
}
@Override
public void onResponse(Response response) throws IOException {
byte [] data = response.body().bytes();
ImgLoader imgLoader = new ImgLoader(title, data);
mImgList.add(imgLoader);
mCountLatch.countDown();
}
}
}
AsyncTask
public class LoadTask extends AsyncTask {
private static final String TAG = "LoadTask";
private Context context;
public LoadTask(Context context) {
this.context = context;
}
@Override
protected Object doInBackground(Object[] objects) {
Log.d(TAG, "OKHttpHelper");
new OKHttpHelper(context).request();
return null;
}
}
RecyclerView
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private static final String TAG = "RecyclerAdapter";
private List<ImgLoader> mImgList;
public RecyclerAdapter(List<ImgLoader> list) {
mImgList = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_layout,
parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ImgLoader loader = mImgList.get(position);
Glide.with(holder.itemView).load(loader.data).into(holder.imageView);
holder.textView.setText(loader.name);
}
@Override
public int getItemCount() {
return mImgList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;
public ViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.img);
textView = itemView.findViewById(R.id.text);
}
}
}
Parcel
public class ImgLoader implements Parcelable {
public String name;
public byte[] data;
public ImgLoader(String name, byte[] data) {
this.name = name;
this.data = data;
}
protected ImgLoader(Parcel in) {
name = in.readString();
data = in.readBlob();
}
public static final Creator<ImgLoader> CREATOR = new Creator<ImgLoader>() {
@Override
public ImgLoader createFromParcel(Parcel in) {
return new ImgLoader(in);
}
@Override
public ImgLoader[] newArray(int size) {
return new ImgLoader[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeBlob(data);
}
}
参考:
关于 Glide
Google推荐——Glide使用详解
简单跨进程使用EventBus