首页 > 学院 > 开发设计 > 正文

Volley 快捷使用

2019-11-09 17:15:30
字体:
来源:转载
供稿:网友

很多人使用volley进行http请求,这里我介绍一下一种相对快捷的使用方式。 先来看一下最终效果:

PRivate void httpRequest() { MyHttpRequest.getInstance().loginRequest(this, "phone", "code", new ZYHttpHandler<User>(this) { @Override public void onGetDataSuccess(User data) { //todo 这里进行数据处理 } }); }

只需传入相应的参数,指定返回结果的类型,就能得到相应的bean。 下面详细看看里面的内容,先来看MyHttpRequest:

/** * 登录接口 */public void loginRequest(Context context, String phone, String verifyCode, ZYHttpHandler<User> handler) { LinkedHashMap<String, String> params = new LinkedHashMap<>(); params.put("phoneNum", phone); params.put("verifyCode", verifyCode); MyClient.getInstance() .post(context, "login_url", params, handler); }

里面很简单,只进行参数的处理和引用接口,个人觉得写在一起还是很方便的。 接下来看MyClient,这个里面就是使用volley的基本写法,首先看post请求:

/** * post请求 * * @param url 请求地址 * @param params 请求参数 * @param handler 响应handler */ public void post(Context context, String url, HashMap<String, String> params, ZYHttpHandler<?> handler) { String URL = getUrlWithQueryString(true, url, params); LogUtils.e(URL); if (!NetworkUtils.hasNetWork(context)) { handler.onFinish(); handler.onFailure(-1, "", "请检查网络", null); } else { if (params == null) { params = new LinkedHashMap<>(); } JSONObject jsonObject = new JSONObject(params); JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, handler, handler) { @Override public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<>(); headers.put("Accept", "application/json"); headers.put("Content-Type", "application/json; charset=UTF-8"); return headers; } }; mRequestQueue.add(jsonRequest); } }

没有什么特别的,就是对volley的简单使用: get请求:

/** * get请求 */ public void get(Context context, String url, HashMap<String, String> params, ZYHttpHandler<?> handler) { String URL = getUrlWithQueryString(true, url, params); LogUtils.e(URL); if (!NetworkUtils.hasNetWork(context)) { handler.onFinish(); handler.onFailure(-1, "", "请检查网络", null); return; } JsonRequest jsonRequest = new JsonObjectRequest(url, handler, handler) { @Override public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<>(); headers.put("Accept", "application/json"); headers.put("Content-Type", "application/json; charset=UTF-8"); return headers; } }; mRequestQueue.add(jsonRequest); }

终于到了最后的核心—ZYHttpHandler:

public abstract class ZYHttpHandler<T> implements Response.Listener<JSONObject>, Response.ErrorListener

T指定范性,使用时传入相应的bean,获取json数据成功后解析时用; implements Response.Listener、Response.ErrorListener是volley所用的参数,各自有对应的成功(onResponse)、失败方法(onErrorResponse)。 json数据请求成功会调用onResponse方法,这里能得到数据就证明volley使用是正确的,至于是不是你想要的格式,还要看看你的参数对不对。

@Override public void onResponse(JSONObject jsonObject) { try { LogUtils.e(jsonObject.toString()); Type type = getType(); if (jsonObject.has("code") && jsonObject.getString("code").equals("0")) { Gson gson = new Gson(); if (!jsonObject.has("content")) { onGetDataSuccess(null); onFinish(); return; } T t; String data = jsonObject.getString("content"); if (data.equals("")) { t = null; } else { t = gson.fromJson(data, type); } onGetDataSuccess(t); } else { if (jsonObject.has("code") && jsonObject.has("content")) { onFailure(0, null, jsonObject.getString("content"), null); } else if (jsonObject.has("content")) { onFailure(0, null, jsonObject.getString("content"), null); } else if (jsonObject.has("code")) { //没有错误信息则不弹出提示 //onFailure(jsonObject.getInt("code"), null, null, null); } } } catch (Exception e) { e.printStackTrace(); onFailure(0, null, "", e); } onFinish(); }

拿到json数据后,首先进行简单的解析,然后用gson对content里面的数据进行解析,将得到的bean传入onGetDataSuccess方法。这样写的话,对所有数据都进行了统一的解析处理,gson调用一次够了,所有的错误情况进行统一处理。 ps:代码里面的json数据是我随意创造的,具体情况具体对待,有的json数据content里面一会是jsonObject,一会是jsonArray,这种情况你需要和你们后台谈谈了,或者加一个判断。 失败方法onErrorResponse:

@Override public void onErrorResponse(VolleyError error) { try { if (error.toString().equals("com.android.volley.TimeoutError")) { onFailure(-1, null, "请求超时", null); } else { onFailure(-1, null, error.getMessage(), null); } } catch (Exception e) { e.printStackTrace(); onFailure(-1, null, e.getMessage(), e); } }

至此,volley快捷使用介绍完毕,使用起来顺手多了。 下面附上源码及volley架包:链接


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表