volley的正确使用方式
来源:互联网 发布:java bio nio aio详解 编辑:程序博客网 时间:2024/04/30 08:39
源码下载:https://github.com/heinika/android-volley
导入的三种方式:
在dependencies下搜索
导入源码包,import model 在dependencies导入model导入jar,放在lib目录下
networkimageview防止内存溢出
先下载,判断内存,不够就压缩图片
volley实现方式(如上图):
先找缓存
后访问网络
使用起来非常简单:
先写一个单例,保证只有一个RequestQueue
public class MySingleton {再在主函数中使用
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private MySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized MySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new MySingleton(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
case R.id.button_volley_connect:
/**
* 应该创建单例防止出现多个RequestQueue
*/
// RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
MySingleton singleton = MySingleton.getInstance(getApplicationContext());
RequestQueue queue = singleton.getRequestQueue();
StringRequest request = new StringRequest(Request.Method.POST, "http://192.168.0.30:8080/MyWebTest/MyTestServerlet", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mTextViewVolley.setText(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextViewVolley.setText("网络连接失败"+error.toString());
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap map = new HashMap();
map.put("username","heinika");
return map;
}
};
singleton.addToRequestQueue(request);
break;
case R.id.button_networkimageview:
mNetworkImageView.setImageUrl("http://f.hiphotos.baidu.com/news/w%3D638/sign=38984923f5deb48ffb69a2ddc81e3aef/b03533fa828ba61e6db11b6d4734970a304e5911.jpg"
,MySingleton.getInstance(getApplicationContext()).getImageLoader());
break;
default:
break;
}
}
0 0
- volley的正确使用方式
- Volley的使用方式
- 用正确的姿势使用volley
- 关于Volley的使用方式
- eclipse的正确使用方式
- Fragment的正确使用方式
- Xshell的正确使用方式
- PHP引号的正确使用方式介绍
- 关于urllib2.open的正确使用方式
- PHP引号的正确使用方式介绍
- PullToRefreshListView 使用 onItemLongClick 的正确打开方式
- 论strpos 的正确使用方式
- DAY17_openssl的正确打开使用方式
- 使用SpringJDBC的正确开启方式
- Spark Accumulator的正确使用方式
- bufferknife框架的正确使用方式
- iOS - Masonry的正确使用方式
- bootstrap-datepicker 的正确使用方式
- Windows Socket 编程_ 简单的服务器/客户端程序
- UVa 12569:Planning mobile robot on Tree(EASY Version)(BFS)
- python_docx
- 黑马程序员-------C语言----const修饰符
- POJ2774 Long Long Message(后缀数组入门题)
- volley的正确使用方式
- Java WEB 总结 JSP篇
- 老师语录
- TCP/IP 相关总结
- java实现大数加法
- 理论知识总结
- 第八周第一天
- 南阳理工NYOJ 975关于521
- HDU--1213并查集