You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuRequest.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package com.luticate.utils.dataaccess;
  2. import android.content.Context;
  3. import android.net.Uri;
  4. import com.android.volley.DefaultRetryPolicy;
  5. import com.android.volley.Request;
  6. import com.android.volley.RequestQueue;
  7. import com.android.volley.Response;
  8. import com.android.volley.VolleyError;
  9. import com.android.volley.toolbox.StringRequest;
  10. import com.android.volley.toolbox.Volley;
  11. import com.luticate.utils.business.LuPromise;
  12. import com.luticate.utils.dbo.LuDataAccessConfigDbo;
  13. import com.luticate.utils.dbo.LuDbo;
  14. import com.luticate.utils.dbo.LuPromiseError;
  15. import org.json.JSONObject;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. /**
  19. *
  20. * Created by robin on 11/27/15.
  21. */
  22. public class LuRequest {
  23. private static RequestQueue _requestQueue = null;
  24. public static void init(Context ctx)
  25. {
  26. _requestQueue = Volley.newRequestQueue(ctx.getApplicationContext());
  27. }
  28. protected static <T extends LuDbo> Response.Listener<String> getListener(final Class<T> type, final LuPromise<T> promise)
  29. {
  30. return new Response.Listener<String>()
  31. {
  32. @Override
  33. public void onResponse(String response) {
  34. try {
  35. T dbo = type.newInstance();
  36. JSONObject json = new JSONObject(response);
  37. JSONObject obj = null;
  38. if (json.has("data")) {
  39. obj = json.optJSONObject("data");
  40. if (obj == null) {
  41. obj = new JSONObject();
  42. obj.put("value", json.opt("data"));
  43. }
  44. }
  45. dbo.fromJson(obj);
  46. promise.resolve(dbo);
  47. } catch (Exception e)
  48. {
  49. e.printStackTrace();
  50. promise.reject(new LuPromiseError("Failed to parse success server response", 200));
  51. }
  52. }
  53. };
  54. }
  55. protected static <T extends LuDbo> Response.ErrorListener getErrorListener(final LuPromise<T> promise)
  56. {
  57. return new Response.ErrorListener() {
  58. @Override
  59. public void onErrorResponse(VolleyError error) {
  60. int code = 0;
  61. try
  62. {
  63. if (error != null) {
  64. if (error.networkResponse != null)
  65. {
  66. code = error.networkResponse.statusCode;
  67. JSONObject data = new JSONObject(new String(error.networkResponse.data));
  68. promise.reject(new LuPromiseError(data.getString("message"), code));
  69. }
  70. else {
  71. if (error.getCause() != null) {
  72. promise.reject(new LuPromiseError(error.getCause().getMessage(), code));
  73. }
  74. else {
  75. promise.reject(new LuPromiseError(error.toString(), code));
  76. }
  77. }
  78. }
  79. else {
  80. promise.reject(new LuPromiseError("Unknown network error", code));
  81. }
  82. } catch (Exception e)
  83. {
  84. promise.reject(new LuPromiseError(e.getMessage(), code));
  85. }
  86. }
  87. };
  88. }
  89. protected static <T extends LuDbo> LuPromise<T> request(final LuDataAccessConfigDbo config,
  90. int method, Class<T> type, String url,
  91. final HashMap<String, String> params)
  92. {
  93. LuPromise<T> promise = new LuPromise<>();
  94. StringRequest request = new StringRequest(method, config.getBaseUrl() + url,
  95. getListener(type, promise), getErrorListener(promise))
  96. {
  97. @Override
  98. public String getBodyContentType() {
  99. return "application/x-www-form-urlencoded; charset=UTF-8";
  100. }
  101. @Override
  102. protected Map<String, String> getParams() {
  103. return params;
  104. }
  105. @Override
  106. public Map<String, String> getHeaders() {
  107. return config.getHeaders();
  108. }
  109. };
  110. request.setRetryPolicy(new DefaultRetryPolicy(10000,
  111. DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
  112. DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
  113. _requestQueue.add(request);
  114. return promise;
  115. }
  116. public static <T extends LuDbo> LuPromise<T> get(final LuDataAccessConfigDbo config, Class<T> type,
  117. String url, final HashMap<String, String> getParams)
  118. {
  119. Uri.Builder uri = Uri.parse(url).buildUpon();
  120. for (String key : getParams.keySet())
  121. {
  122. uri.appendQueryParameter(key, getParams.get(key));
  123. }
  124. LuPromise<T> promise = new LuPromise<>();
  125. StringRequest request = new StringRequest(Request.Method.GET, config.getBaseUrl() + uri.toString(),
  126. getListener(type, promise), getErrorListener(promise))
  127. {
  128. @Override
  129. public Map<String, String> getHeaders() {
  130. return config.getHeaders();
  131. }
  132. };
  133. _requestQueue.add(request);
  134. return promise;
  135. }
  136. public static <T extends LuDbo> LuPromise<T> get(LuDataAccessConfigDbo config, Class<T> type,
  137. String url)
  138. {
  139. return get(config, type, url, new HashMap<String, String>());
  140. }
  141. public static <T extends LuDbo> LuPromise<T> post(LuDataAccessConfigDbo config, Class<T> type,
  142. String url, final HashMap<String, String> postParams)
  143. {
  144. return request(config, Request.Method.POST, type, url, postParams);
  145. }
  146. public static <T extends LuDbo> LuPromise<T> post(LuDataAccessConfigDbo config, Class<T> type,
  147. String url)
  148. {
  149. return post(config, type, url, new HashMap<String, String>());
  150. }
  151. }