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.business;
  2. import android.content.Context;
  3. import android.net.Uri;
  4. import com.android.volley.AuthFailureError;
  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.dbo.LuDataAccessConfigDbo;
  12. import com.luticate.utils.dbo.LuDbo;
  13. import org.json.JSONObject;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. /**
  17. *
  18. * Created by robin on 11/27/15.
  19. */
  20. public class LuRequest {
  21. private static RequestQueue _requestQueue = null;
  22. public static void init(Context ctx)
  23. {
  24. _requestQueue = Volley.newRequestQueue(ctx.getApplicationContext());
  25. }
  26. protected static <T extends LuDbo> Response.Listener<String> getListener(final Class<T> type, final LuPromise<T> promise)
  27. {
  28. return new Response.Listener<String>()
  29. {
  30. @Override
  31. public void onResponse(String response) {
  32. try {
  33. T dbo = type.newInstance();
  34. try {
  35. JSONObject json = new JSONObject(response);
  36. JSONObject obj = null;
  37. if (!json.isNull("data")) {
  38. obj = json.optJSONObject("data");
  39. if (obj == null) {
  40. obj = new JSONObject();
  41. obj.put("value", json.opt("data"));
  42. }
  43. }
  44. dbo.fromJson(obj);
  45. promise.resolve(dbo);
  46. } catch (Exception e)
  47. {
  48. e.printStackTrace();
  49. promise.reject(new LuPromise.LuPromiseError("Failed to parse success server response", 200));
  50. }
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. promise.reject(new LuPromise.LuPromiseError("Failed to initialize server response", 200));
  54. }
  55. }
  56. };
  57. }
  58. protected static <T extends LuDbo> Response.ErrorListener getErrorListener(final LuPromise<T> promise)
  59. {
  60. return new Response.ErrorListener() {
  61. @Override
  62. public void onErrorResponse(VolleyError error) {
  63. int code = 0;
  64. try
  65. {
  66. if (error != null) {
  67. if (error.networkResponse != null)
  68. {
  69. code = error.networkResponse.statusCode;
  70. JSONObject data = new JSONObject(new String(error.networkResponse.data));
  71. promise.reject(new LuPromise.LuPromiseError(data.getString("Message"), code));
  72. }
  73. else {
  74. if (error.getCause() != null) {
  75. promise.reject(new LuPromise.LuPromiseError(error.getCause().getMessage(), code));
  76. }
  77. else {
  78. promise.reject(new LuPromise.LuPromiseError(error.toString(), code));
  79. }
  80. }
  81. }
  82. else {
  83. promise.reject(new LuPromise.LuPromiseError("Unknown network error", code));
  84. }
  85. } catch (Exception e)
  86. {
  87. promise.reject(new LuPromise.LuPromiseError(e.getMessage(), code));
  88. }
  89. }
  90. };
  91. }
  92. protected static <T extends LuDbo> LuPromise<T> request(final LuDataAccessConfigDbo config,
  93. int method, Class<T> type, String url,
  94. final HashMap<String, String> params)
  95. {
  96. LuPromise<T> promise = new LuPromise<>();
  97. StringRequest request = new StringRequest(method, config.getBaseUrl() + url,
  98. getListener(type, promise), getErrorListener(promise))
  99. {
  100. @Override
  101. public String getBodyContentType() {
  102. return "application/x-www-form-urlencoded; charset=UTF-8";
  103. }
  104. @Override
  105. protected Map<String, String> getParams() {
  106. return params;
  107. }
  108. @Override
  109. public Map<String, String> getHeaders() {
  110. return config.getHeaders();
  111. }
  112. };
  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. }