时之沙 / blog_20131218_10_2848867

来自CSDN博客:Android 网络通信框架Volley简介 http://blog.csdn.net/ai_yong_jie/article/details/51240467#

  最后更新时间 2016-04-25 10:35:48
blog_20131218_1_1922749 1行 Java
Raw
 1
git clone https://android.googlesource.com/platform/frameworks/volley
blog_20131218_2_1954986 9行 Java
Raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
mQueue = Volley.newRequestQueue(getApplicationContext());
mQueue.add(new JsonObjectRequest(Method.GET, url, null,
new Listener() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "response : " + response.toString());
}
}, null));
mQueue.start();
blog_20131218_3_5716977 5行 Java
Raw
 1
 2
 3
 4
 5
// imageView是一个ImageView实例
// ImageLoader.getImageListener的第二个参数是默认的图片resource id
// 第三个参数是请求失败时候的资源id,可以指定为0
ImageListener listener = ImageLoader.getImageListener(imageView, android.R.drawable.ic_menu_rotate, android.R.drawable.ic_delete);
mImageLoader.get(url, listener);
blog_20131218_4_1638559 1行 Java
Raw
 1
mImageView.setImageUrl(url, imageLoader)
blog_20131218_5_5400549 7行 Java
Raw
 1
 2
 3
 4
 5
 6
 7
mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache());
... ...
if(holder.imageRequest != null) {
holder.imageRequest.cancel();
}
holder.imageRequest = mImageLoader.get(BASE_UR + item.image_url, holder.imageView, R.drawable.loading, R.drawable.error);
blog_20131218_6_4910181 1行 Java
Raw
 1
mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache());
blog_20131218_7_831763 9行 Java
Raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
/**
* Simple cache adapter interface. If provided to the ImageLoader, it
* will be used as an L1 cache before dispatch to Volley. Implementations
* must not block. Implementation with an LruCache is recommended.
*/
public interface ImageCache {
public Bitmap getBitmap(String url);
public void putBitmap(String url, Bitmap bitmap);
}
blog_20131218_8_2641907 13行 Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(
response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(
gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}
blog_20131218_9_3339235 8行 Java
Raw
 1
 2
 3
 4
 5
 6
 7
 8
mRequestQueue.add( new GsonRequest(url, ListResponse.class, null,
new Listener() {
public void onResponse(ListResponse response) {
appendItemsToList(response.item);
notifyDataSetChanged();
}
}
}
blog_20131218_10_2848867 6行 Java
Raw
 1
 2
 3
 4
 5
 6
@Override public void onPostExecute(Result r) {
if (getActivity() == null) {
return;
}
// ...
}
blog_20131218_11_3022808 7行 Java
Raw
 1
 2
 3
 4
 5
 6
 7
@Override
public void onStop() {
for (Request <?> req : mInFlightRequests) {
req.cancel();
}
...
}
blog_20131218_12_5356339 4行 Java
Raw
 1
 2
 3
 4
@Override pubic void onStop() {
mRequestQueue.cancelAll(this);
...
}
blog_20131218_13_4342583 6行 Java
Raw
 1
 2
 3
 4
 5
 6
@Override public void onStop() {
mRequestQueue.cancelAll( new RequestFilter() {})
...
// or
mRequestQueue.cancelAll(new Object());
...