Volley 网络访问
来源:互联网 发布:linux系统下载 编辑:程序博客网 时间:2024/05/07 09:56
对Volley进行封装
public class VolleyManager { /** * 请求队列 */ private static RequestQueue mRequestQueue; /** * 图片加载工具类 */ private static ImageLoader mImageLoader; private VolleyManager() { // no instances } /** * 使用Volley请求网络和图片是首先调用该方法 * 建议在Application 的onCreater中调用 * @param context */ public static void init(Context context) { mRequestQueue = Volley.newRequestQueue(context); //告诉你你的机器还有多少内存,在计算缓存大小的时候会比较有 int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)) .getMemoryClass(); // Use 1/8th of the available memory for this memory cache. int cacheSize = 1024 * 1024 * memClass / 8;//图片缓存的空间 mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache(cacheSize)); } /** * 得到消息队列 * @return */ public static RequestQueue getRequestQueue() { if (mRequestQueue != null) { return mRequestQueue; } else { throw new IllegalStateException("RequestQueue not initialized"); } } /** * 把请求添加到队列中 * @param request * @param tag */ public static void addRequest(Request<?> request, Object tag) { if (tag != null) { request.setTag(tag); } mRequestQueue.add(request); } /** * 请求的取消 * @param tag */ public static void cancelAll(Object tag) { mRequestQueue.cancelAll(tag); } /** * Returns instance of ImageLoader initialized with {@see FakeImageCache} * which effectively means that no memory caching is used. This is useful * for images that you know that will be show only once. * * @return */ public static ImageLoader getImageLoader() { if (mImageLoader != null) { return mImageLoader; } else { throw new IllegalStateException("ImageLoader not initialized"); } }}
使用Volley
private void getDataFromNetByVolley() {// RequestQueue queue = Volley.newRequestQueue(mContext); //单例模式,在Application中初始化 StringRequest request = new StringRequest(Request.Method.GET, Constants.PHOTOSDETAIL_URL, new Response.Listener<String>() { @Override public void onResponse(String result) { //保存数据// CacheUtils.putString(mContext, Constants.NEWSCENTER_URL, result);// //主线程 processData(result); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { LogUtil.e("volley新闻中心联网失败=" + volleyError.getMessage()); } }){//处理乱码问题 @Override protected Response<String> parseNetworkResponse(NetworkResponse response) { try { String parsed=new String(response.data,"UTF-8"); return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return super.parseNetworkResponse(response); } };// queue.add(request); //把请求加入到队列中 VolleyManager.addRequest(request,"PhotoDetail"); }
在Application中初始化volley
public class BeijingNewsApplication extends Application{ @Override public void onCreate() { super.onCreate(); x.Ext.init(this); x.Ext.setDebug(true); VolleyManager.init(this); //volley初始化 ShareSDK.initSDK(this); }}
0 0
- Android Volley网络访问
- Volley 网络访问
- 网络访问之Volley
- Volley简单访问网络数据
- Android_访问网络四(Volley网络框架)
- Android 网络访问框架 Volley 基本使用
- Volley框架网络访问String,JsonObject,JsonArray
- volley,访问网络,聚合天气数据
- 用volley框架进行网络访问
- volley由于网络访问比较慢,导致访问两次的现象
- 访问网络-------开源-Volley(Google亲儿子)
- volley+okhttp封装,一行代码就可访问网络
- 使用Volley框架发起网络访问的写法
- 【Android应用】AS Volley网络访问 简单的二次封装
- 访问网络的框架(Volley和OkHttp3)使用
- 【网络通信:Volley】Volley开篇
- Volley访问网络,进行精致封装,在需要访问的时候,直接调用
- Volley访问HTTPS
- 单点登陆的三种实现方式
- 【GPUView】GPUView的相关资料摘录
- BootLoader的启动流程之我见
- ASP.NET MVC修改命名空间后报错,未能加载类型'WebApplication1.MvcApplication“
- svn环境搭建及使用
- Volley 网络访问
- UserDefault实现数据储存
- Unity Input 通过蓝牙手柄控制游戏
- 装饰者模式.继承.动态代理
- html中点击a标签获取text
- 通过npm使用React
- const细谈--自我品味
- oracle--plsql2---函数、包、变量
- PL SQL Developer 使用总结