Android 网络通信框架Volley简介
来源:互联网 发布:机器人发音软件 编辑:程序博客网 时间:2024/06/01 23:27
在ListAdapter#getView()里开始图像的读取。
通过AsyncTask等机制使用HttpURLConnection从服务器去的图片资源
在AsyncTask#onPostExecute()里设置相应ImageView的属性。
JSON,图像等的异步下载;
网络请求的排序(scheduling)
网络请求的优先级处理
缓存
多级别取消请求
和Activity和生命周期的联动(Activity结束时同时取消所有网络请求)
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();
// 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);
mImageView.setImageUrl(url, imageLoader)
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);
mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache());
/**
* 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);
}
@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));
}
}
mRequestQueue.add( new GsonRequest(url, ListResponse.class, null,
new Listener() {
public void onResponse(ListResponse response) {
appendItemsToList(response.item);
notifyDataSetChanged();
}
}
}
@Override public void onPostExecute(Result r) {
if (getActivity() == null) {
return;
}
// ...
}
@Override
public void onStop() {
for (Request <?> req : mInFlightRequests) {
req.cancel();
}
...
}
@Override pubic void onStop() {
mRequestQueue.cancelAll(this);
...
}
@Override public void onStop() {
mRequestQueue.cancelAll( new RequestFilter() {})
...
// or
mRequestQueue.cancelAll(new Object());
...
JSON对象
图片加载
- Android 网络通信框架Volley简介
- Android 网络通信框架Volley简介
- Android 网络通信框架Volley简介
- Android 网络通信框架Volley简介
- Android 网络通信框架Volley简介
- Android 网络通信框架Volley简介
- Android 网络通信框架Volley简介(Google IO 2013)
- Android网络通信框架Volley简介(Google IO 2013)
- Android网络通信框架Volley简介(Google IO 2013)
- Android 网络通信框架Volley简介(Google IO 2013)
- Android 网络通信框架Volley简介(Google IO 2013)
- Android 网络通信框架Volley简介(Google IO 2013)
- Android 网络通信框架Volley简介(Google IO 2013)
- Android 网络通信框架Volley简介(Google IO 2013)
- 推荐:Android 网络通信框架Volley简介(Google IO 2013)
- Android 网络通信框架Volley简介(Google IO 2013)
- Android 网络通信框架Volley简介(Google IO 2013)
- Android 网络通信框架Volley简介(Google IO 2013)
- 单例模式
- 建立WordPress博客
- 日经春秋 20150825
- AFN基本使用
- OSI七层模型详解
- Android 网络通信框架Volley简介
- 日经社説 20150825 北方領土交渉の厳しい現実
- mysql安装图解 mysql图文安装教程(详细说明)
- dev view 列名称居中
- window 环境下 apache james 3.0-beta4 安装、搭建、运行。
- opencv Bmp和opencv格式转换
- SSi框架整合
- eclipse导入一个项目之后没有JRE System Library怎么办?
- Oracle 11g Express Edition 安装及常见问题