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

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