自己对volley http模块的二次封装的使用

本文详细介绍了如何使用Volley库进行网络请求,包括添加依赖、配置请求队列、实现请求队列管理、封装与服务器交互的方法以及如何在程序中调用这些方法进行数据加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

volley传送门

1.添加依赖

compile ‘com.mcxiaoke.volley:library:1.0.19’已弃用,现在用
compile ‘com.android.volley:volley:1.0.0’

2.copy文件夹http_volley

volley文件夹

3.在application里

public static final String TAG = CGBapplication.class.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
...
 public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public ImageLoader getImageLoader() {
        getRequestQueue();
        if (mImageLoader == null) {
            mImageLoader = new ImageLoader(this.mRequestQueue,
                    new LruBitmapCache());
        }
        return this.mImageLoader;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        // set the default tag if tag is empty
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }

4.在BaseActivity或BaseFragment中

    //*************与服务器交互封装start************//

    public void loadData(String reqTag, RequestInfo reqInfo, boolean isShowDialog,
                         boolean cancelable, VolleyResponse.strReqCallback callback){
        if (DeviceUtils.isHasNetWork()) {
            //            reqInfo.getBodyParams().put(); //添加公共参数
            VolleyLoader.start(mContext).post(reqTag, reqInfo, isShowDialog, cancelable, callback);
        } else {
            showToast("请检查网络连接");
        }
    }
    public void showToast(String content) {
        Toast.makeText(mContext, content, Toast.LENGTH_SHORT).show();
    }

5.在程序中用的时候

     RequestInfo requestInfo = new RequestInfo(URLS.LOGIN);

                requestInfo.addBodyParams("username", "13203811868");
                requestInfo.addBodyParams("password","a123456");
                requestInfo.addBodyParams("grant_type","password");
                requestInfo.addBodyParams("client_secret","123@abc");
                requestInfo.addBodyParams("client_id","consoleApp");
                loadData(VolleyTag, requestInfo, true, true, new VolleyResponse.strReqCallback() {
                    @Override
                    public void success(String response) {
                        LoginModel login = JSON.parseObject(response, LoginModel.class);
                        if(login.getError()==null||login.getError().isEmpty()){
                            Intent toMain = new Intent(mContext, MainActivity.class);
                            startActivity(toMain);
                            finish();
                        }else{
                            showToast(login.getError_description());
                        }
                    }

                    @Override
                    public void error(VolleyError error) {

                    }
                });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值