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
原创粉丝点击