Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

LuRequest.java 5.3KB

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