Volley 网络访问

对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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值