PageRenderTime 1285ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/src/main/java/com/uprint/android_pack/cloudprint4androidmanager/network/VolleyHelper.java

https://gitlab.com/zhangxiaang/CloudPrint4AndroidManager
Java | 139 lines | 121 code | 15 blank | 3 comment | 10 complexity | 3f53cd60818bdd060f8611a413321b8c MD5 | raw file
  1. package com.uprint.android_pack.cloudprint4androidmanager.network;
  2. import android.content.Context;
  3. import android.os.Message;
  4. import com.alibaba.fastjson.JSON;
  5. import com.android.volley.AuthFailureError;
  6. import com.android.volley.NetworkError;
  7. import com.android.volley.NoConnectionError;
  8. import com.android.volley.RedirectError;
  9. import com.android.volley.Request;
  10. import com.android.volley.RequestQueue;
  11. import com.android.volley.Response;
  12. import com.android.volley.ServerError;
  13. import com.android.volley.TimeoutError;
  14. import com.android.volley.VolleyError;
  15. import com.android.volley.toolbox.JsonObjectRequest;
  16. import com.android.volley.toolbox.StringRequest;
  17. import com.android.volley.toolbox.Volley;
  18. import com.uprint.android_pack.cloudprint4androidmanager.CPApplication;
  19. import com.uprint.android_pack.cloudprint4androidmanager.utils.EmptyViewUtil;
  20. import com.uprint.android_pack.cloudprint4androidmanager.utils.MapFormatUtil;
  21. import com.uprint.android_pack.cloudprint4androidmanager.utils.NetworkUtil;
  22. import org.json.JSONObject;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. /**
  26. * Created by zhangxiaang on 15/10/13.
  27. */
  28. public class VolleyHelper {
  29. private RequestQueue requestQueue;
  30. private CommonHandler mHandler;
  31. private Context mContext;
  32. public VolleyHelper(CommonHandler mHandler, Context mContext) {
  33. this.mHandler = mHandler;
  34. this.mContext = mContext;
  35. }
  36. public RequestQueue getRequestQueue() {
  37. if (requestQueue == null) {
  38. requestQueue = Volley.newRequestQueue(CPApplication.getmInstance());
  39. }
  40. return requestQueue;
  41. }
  42. public void getAsString(String url) {
  43. if (!NetworkUtil.checkNetWork(mContext)) {
  44. mHandler.createExceptionMsg(new NetworkError());
  45. NetworkUtil.noNetWork(mContext);
  46. return;
  47. }
  48. EmptyViewUtil.allowed();
  49. StringRequest request = new StringRequest(Request.Method.GET, url
  50. , resListener, errorListener);
  51. getRequestQueue().add(request);
  52. }
  53. public void postAsJson(String url, Map<String, String> params_map) {
  54. if (!NetworkUtil.checkNetWork(mContext)) {
  55. NetworkUtil.noNetWork(mContext);
  56. mHandler.createExceptionMsg(new NetworkError());
  57. return;
  58. }
  59. EmptyViewUtil.allowed();
  60. JSONObject jsonObject = new JSONObject(params_map);
  61. JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonObject
  62. , jresListener, errorListener) {
  63. @Override
  64. public Map<String, String> getHeaders() {
  65. HashMap<String, String> headers = new HashMap<>();
  66. headers.put("Accept", "application/json");
  67. headers.put("Content-Type", "application/json; charset=UTF-8");
  68. return headers;
  69. }
  70. };
  71. getRequestQueue().add(request);
  72. }
  73. public void getWithParams(String url, final Map<String, String> request_params) {
  74. if (!NetworkUtil.checkNetWork(mContext)) {
  75. NetworkUtil.noNetWork(mContext);
  76. mHandler.createExceptionMsg(new NetworkError());
  77. return;
  78. }
  79. EmptyViewUtil.allowed();
  80. StringRequest request = new StringRequest(Request.Method.GET, MapFormatUtil.urlEncodeUTF8(url, request_params)
  81. , resListener, errorListener);
  82. getRequestQueue().add(request);
  83. }
  84. private Response.Listener<JSONObject> jresListener = new Response.Listener<JSONObject>() {
  85. @Override
  86. public void onResponse(JSONObject response) {
  87. Message msg = mHandler.obtainMessage();
  88. msg.what = NetValue.STATUS_SUCCESS;
  89. msg.obj = JSON.parseObject(response.toString());
  90. mHandler.handleMessage(msg);
  91. }
  92. };
  93. private Response.Listener<String> resListener = new Response.Listener<String>() {
  94. @Override
  95. public void onResponse(String response) {
  96. Message msg = mHandler.obtainMessage();
  97. msg.what = NetValue.STATUS_SUCCESS;
  98. com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(response);
  99. msg.obj = jsonObject;
  100. mHandler.handleMessage(msg);
  101. }
  102. };
  103. private Response.ErrorListener errorListener = new Response.ErrorListener() {
  104. @Override
  105. public void onErrorResponse(VolleyError error) {
  106. if (error instanceof TimeoutError) {
  107. mHandler.createExceptionMsg(new TimeoutError());
  108. }
  109. if (error instanceof NetworkError) {
  110. mHandler.createExceptionMsg(new NetworkError());
  111. }
  112. if (error instanceof AuthFailureError) {
  113. mHandler.createExceptionMsg(new AuthFailureError("请重新登录"));
  114. }
  115. if (error instanceof NoConnectionError) {
  116. mHandler.createExceptionMsg(new NoConnectionError());
  117. }
  118. if (error instanceof RedirectError) {
  119. mHandler.createExceptionMsg(new RedirectError());
  120. } else
  121. mHandler.createExceptionMsg(new ServerError());
  122. }
  123. };
  124. }