Volley之简单封装(2)
来源:互联网 发布:java md5 编辑:程序博客网 时间:2024/05/21 17:07
本文对Volley简单封装,本文只是记录volley的使用
主要请求内部实现加载进度条:
数据回调的class
public abstract class CallBack { protected void onFail(Context context,int errCode,String errMsg) { } public abstract void onSuccess(JSONObject json) throws JSONException; protected void onCancel(){ //TODO 请求取消处理 } //返回键是否能取消请求 protected boolean canCancel(){ return true; }}
单例请求class:
public class HttpUtils { private static HttpUtils ourInstance = new HttpUtils(); private RequestQueue requestQueue = Volley.newRequestQueue(App.app); private ProgressDialog dialog; private Object tag; public static HttpUtils getInstance() { return ourInstance; } private HttpUtils() { } //根据需求添加重载方法 public void Post( String url, final Map<String,String> map, final CallBack callBack) { makeRequest(Request.Method.POST,url,map,callBack,null,null,false); } private void makeRequest(int method, String url, final Map<String,String> map, final CallBack callBack, final Object tag, Context context, boolean showLoad){ final StringRequest request = new StringRequest(method, url, new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONObject object = new JSONObject(response); if (callBack != null) { int code = object.getInt("code"); if (code == 0) { callBack.onSuccess(object); }else callBack.onFail(null,code,object.getString("errMsg")); } } catch (JSONException e) { callBack.onFail(null,-1,"读取服务器数据失败"); } dismissIfNeed(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { if (callBack!=null) { callBack.onFail(null, -2, "请求服务器失败"); } dismissIfNeed(); } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { if (map != null) return map; return super.getParams(); } }; request.setRetryPolicy(new DefaultRetryPolicy(5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); if (tag!=null) { request.setTag(tag); } requestQueue.add(request); dialog = null; if (showLoad && context!=null && tag!=null) { dialog = new ProgressDialog(context); dialog.setCancelable(false); dialog.show(); this.tag = tag; dialog.setOnKeyListener(new DialogInterface.OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == MotionEvent.ACTION_UP) { if (callBack != null && callBack.canCancel()) { callBack.onCancel(); cancelRequest(tag); dismissIfNeed(); } } return false; } }); } } private void dismissIfNeed(){ if (dialog!=null&&dialog.isShowing()) dialog.dismiss(); } public void cancelRequest(Object tag){ requestQueue.cancelAll(tag); if (equals(this.tag,tag)) dismissIfNeed(); } public static boolean equals(Object a, Object b) { return (a == null) ? (b == null) : a.equals(b); } public static final class RequestBuilder{ private int method = Request.Method.POST; private String url; private Map<String,String> map; private CallBack callBack; private Object tag; //----------- private Context context; private boolean showLoad; public void build(){ if (method == Request.Method.GET && map!=null){ url=url+"?"; for (String key:map.keySet()) { try { url=url+key+"="+ URLEncoder.encode(map.get(key),"UTF-8")+"&"; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } url = url.substring(0, url.length()-1); } HttpUtils.getInstance().makeRequest(method,url,map,callBack,tag,context,showLoad); } public RequestBuilder bindMethod(int method){ this.method = method; return this; } public RequestBuilder bindUrl(String url){ this.url = url; return this; } public RequestBuilder bindMap(Map<String,String> map){ this.map = map; return this; } public RequestBuilder bindCallBack( CallBack callBack){ this.callBack = callBack; return this; } public RequestBuilder bindTag( Object tag){ this.tag = tag; return this; } public RequestBuilder bindContext(Context context){ this.context = context; return this; } public RequestBuilder bindShow(boolean showLoad){ this.showLoad = showLoad; return this; } }}
使用方式:
new HttpUtils.RequestBuilder().bindUrl("url") .bindMap(new HashMap<>()) .bindShow(true) .bindContext(this) .bindTag("TAST") .bindCallBack(new CallBack() { @Override public void onSuccess(JSONObject json) throws JSONException { Log.e("TEST",json.toString()); } @Override protected void onCancel() { Log.e("TEST","onCancel--->"); } }).build(); //---------------或者下面这个方式------------- HttpUtils.getInstance().Post("url", new HashMap<String, String>(), new CallBack() { @Override public void onSuccess(JSONObject json) throws JSONException { } });
0 0
- Volley之简单封装(2)
- Volley之简单封装(1)
- Volley简单封装之Gson解析
- Volley简单封装--笔记
- Android:简单封装Volley StringRequest
- Volley简单二次封装代码
- Volley之返回数据封装
- Volley 简单封装调用超简单
- Volley网络请求的简单封装使用
- Volley的使用(三) 简单封装
- 拒绝繁琐,Volley框架的简单封装
- Android 自己简单封装的Volley
- Android HTTPS认证之Volley封装
- Android HTTPS认证之Volley封装
- Volley封装
- Volley的简单使用,和简单二次封装
- 对Volley简单的封装,让网络请求更简单
- Android 网络连接:Volley(齐射)之简单使用
- Zookeeper实战之单机模式
- ettercap+arp毒化嗅探+dns诈骗
- bootstrap的模态框modal的hidden.bs.modal属性
- OBB包围盒及其碰撞检测算法(一)
- 【Android】Android SurfaceFlinger之OpenGL ES
- Volley之简单封装(2)
- sublime text3中配置Java编译环境
- Differences between SLF and Kaldi lattices
- Openresty的开发闭环初探
- propertype深拷贝
- 关于java 异常处理的学习
- 联合独立调研机构三星公布Note7燃损真相
- CRM 2013 - web resource不正确load的解决方法
- rest api+C#