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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. try {
  37. JSONObject json = new JSONObject(response);
  38. JSONObject obj = null;
  39. if (json.has("data")) {
  40. obj = json.optJSONObject("data");
  41. if (obj == null) {
  42. obj = new JSONObject();
  43. obj.put("value", json.opt("data"));
  44. }
  45. }
  46. dbo.fromJson(obj);
  47. promise.resolve(dbo);
  48. } catch (Exception e)
  49. {
  50. e.printStackTrace();
  51. promise.reject(new LuPromiseError("Failed to parse success server response", 200));
  52. }
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. promise.reject(new LuPromiseError("Failed to initialize server response", 200));
  56. }
  57. }
  58. };
  59. }
  60. protected static <T extends LuDbo> Response.ErrorListener getErrorListener(final LuPromise<T> promise)
  61. {
  62. return new Response.ErrorListener() {
  63. @Override
  64. public void onErrorResponse(VolleyError error) {
  65. int code = 0;
  66. try
  67. {
  68. if (error != null) {
  69. if (error.networkResponse != null)
  70. {
  71. code = error.networkResponse.statusCode;
  72. JSONObject data = new JSONObject(new String(error.networkResponse.data));
  73. promise.reject(new LuPromiseError(data.getString("Message"), code));
  74. }
  75. else {
  76. if (error.getCause() != null) {
  77. promise.reject(new LuPromiseError(error.getCause().getMessage(), code));
  78. }
  79. else {
  80. promise.reject(new LuPromiseError(error.toString(), code));
  81. }
  82. }
  83. }
  84. else {
  85. promise.reject(new LuPromiseError("Unknown network error", code));
  86. }
  87. } catch (Exception e)
  88. {
  89. promise.reject(new LuPromiseError(e.getMessage(), code));
  90. }
  91. }
  92. };
  93. }
  94. protected static <T extends LuDbo> LuPromise<T> request(final LuDataAccessConfigDbo config,
  95. int method, Class<T> type, String url,
  96. final HashMap<String, String> params)
  97. {
  98. LuPromise<T> promise = new LuPromise<>();
  99. StringRequest request = new StringRequest(method, config.getBaseUrl() + url,
  100. getListener(type, promise), getErrorListener(promise))
  101. {
  102. @Override
  103. public String getBodyContentType() {
  104. return "application/x-www-form-urlencoded; charset=UTF-8";
  105. }
  106. @Override
  107. protected Map<String, String> getParams() {
  108. return params;
  109. }
  110. @Override
  111. public Map<String, String> getHeaders() {
  112. return config.getHeaders();
  113. }
  114. };
  115. request.setRetryPolicy(new DefaultRetryPolicy(10000,
  116. DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
  117. DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
  118. _requestQueue.add(request);
  119. return promise;
  120. }
  121. public static <T extends LuDbo> LuPromise<T> get(final LuDataAccessConfigDbo config, Class<T> type,
  122. String url, final HashMap<String, String> getParams)
  123. {
  124. Uri.Builder uri = Uri.parse(url).buildUpon();
  125. for (String key : getParams.keySet())
  126. {
  127. uri.appendQueryParameter(key, getParams.get(key));
  128. }
  129. LuPromise<T> promise = new LuPromise<>();
  130. StringRequest request = new StringRequest(Request.Method.GET, config.getBaseUrl() + uri.toString(),
  131. getListener(type, promise), getErrorListener(promise))
  132. {
  133. @Override
  134. public Map<String, String> getHeaders() {
  135. return config.getHeaders();
  136. }
  137. };
  138. _requestQueue.add(request);
  139. return promise;
  140. }
  141. public static <T extends LuDbo> LuPromise<T> get(LuDataAccessConfigDbo config, Class<T> type,
  142. String url)
  143. {
  144. return get(config, type, url, new HashMap<String, String>());
  145. }
  146. public static <T extends LuDbo> LuPromise<T> post(LuDataAccessConfigDbo config, Class<T> type,
  147. String url, final HashMap<String, String> postParams)
  148. {
  149. return request(config, Request.Method.POST, type, url, postParams);
  150. }
  151. public static <T extends LuDbo> LuPromise<T> post(LuDataAccessConfigDbo config, Class<T> type,
  152. String url)
  153. {
  154. return post(config, type, url, new HashMap<String, String>());
  155. }
  156. }