volley-base

volley base类.简单的封装了volley的使用!(过时)

Posted by Mio4kon on 2014-11-09

此base类在jsonRequest的基础上封装,好处:

  1. 使用简单
  2. 容易对所有请求的全局控制,如sessionId过期既要求登陆.请求时弹出progressDialog等.
  3. 对登陆超时进行统一处理,当然也可以自定义处理.

基本的请求方法POST,在这里可以控制如:判断是否有特殊请求(sessionId的请求),如果有进行特殊处理等

/**
 * 
 * @Description: 最基本的
 * @param
 * @author mio4kon
 */
private <T extends BaseResponseBean> void postMethod(String url, final Map<String, String> map,
        Class clazz, final Listener<T> successListener, ErrorListener errorListener,
        final ProgressDialog dialog) {
    isSessionIdRequest = false;
    mCommonErrorListener = new CommonErrorListener();
    LogUtils.d("发送的url:" + url);
    LogUtils.d("发送的post请求参数:" + map.toString());

    /** 判断是否有session请求 */
    Set<Entry<String, String>> entrySet = map.entrySet();
    for (Entry<String, String> entry : entrySet) {
        String key = entry.getKey();
        if ("sessionId".equals(key)) {
            LogUtils.d("BaseEngin:发送的请求带sessionId...");
            isSessionIdRequest = true;
        }
    }

    GsonRequest<T> gsonRequest = null;
    if (isSessionIdRequest) { // 发送带sessionId的请求
        gsonRequest = new GsonRequest<T>(Method.POST, url, clazz, new Listener<T>() {
            @Override
            public void onResponse(T response) {
                if (dialog != null) {
                    dialog.dismiss();
                }
                String flag = response.flag;
                if ("2".equals(flag)) { // 登陆超时
                    UIUtils.showToastSafe("登陆超时,请重新登陆");
                    /** 处理SessionId过期问题 */
                    dealSessionOverTime();
                    return;
                }
                successListener.onResponse(response);
            }

        }, mCommonErrorListener) {

            // post参数
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
    } else {
        gsonRequest = new GsonRequest<T>(Method.POST, url, clazz, new Listener<T>() {

            @Override
            public void onResponse(T response) {
                if (dialog != null) {
                    dialog.dismiss();
                }
                successListener.onResponse(response);
            }
        }, mCommonErrorListener) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
    }
    // 加入队列,勿忘
    mRequestQueue.add(gsonRequest);
}

无progressDialog的请求

/**
 * 
* @Description: Post请求 无进度
* @param     
* @author         mio4kon
 */
private <T extends BaseResponseBean> void postMethodNoProgress(String url,
        final Map<String, String> map, Class clazz, final Listener<T> successListener,
        ErrorListener errorListener) {
    postMethod(url, map, clazz, successListener, errorListener, null);
}

有progressDialog的请求

/**
 * 
 * @Description: Post请求 有进度
 * @param
 * @author mio4kon
 */
private <T extends BaseResponseBean> void postMethodProgress(final String url,
        final Map<String, String> map, final Class clazz, final Listener<T> successListener,
        final ErrorListener errorListener) {
    final ProgressDialog dialog = new ProgressDialog(mContext);
    dialog.setMessage("请稍等..");
    dialog.show();

    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            postMethod(url, map, clazz, successListener, errorListener, dialog);
        }
    }).start();

}

###更多细节请见附件
附件地址