PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/src/main/java/com/lean56/andplug/network/BaseHttpClient.java

https://gitlab.com/chgocn/AndPlug
Java | 193 lines | 135 code | 36 blank | 22 comment | 8 complexity | 841e8f745a3d9b10ab83a64b76618d42 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.lean56.andplug.network;
  2. import android.content.Context;
  3. import com.alibaba.fastjson.JSON;
  4. import com.loopj.android.http.AsyncHttpClient;
  5. import com.loopj.android.http.AsyncHttpResponseHandler;
  6. import com.loopj.android.http.RequestParams;
  7. import com.loopj.android.http.SyncHttpClient;
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.entity.ByteArrayEntity;
  10. /**
  11. * Base Http Client used for Restful API
  12. * see {http://loopj.com/android-async-http/}
  13. *
  14. * 0. static method for common define
  15. * 1. post/get/put/delete method
  16. *
  17. * @author Charles <zhangchaoxu@gmail.com>
  18. */
  19. public abstract class BaseHttpClient {
  20. protected final static String CONTENT_TYPE_JSON = "application/json";
  21. protected final static String CONTENT_TYPE_XML = "application/xml";
  22. public static final String CONTENT_TYPE_JPEG = "image/jpeg";
  23. private static final int MAX_RETRIES = 5; // default 5
  24. private static final int MAX_RETRY_TIMEOUT = 10 * 1000; // default 20s
  25. private static final int MAX_TIMEOUT = 20 * 1000; // default 10s
  26. private static final int MAX_CONNECTIONS = 5; // default 10
  27. // asynchronous http client
  28. protected static AsyncHttpClient asyncClient = new AsyncHttpClient();
  29. // synchronous http client
  30. protected static SyncHttpClient syncClient = new SyncHttpClient();
  31. static {
  32. asyncClient.setMaxRetriesAndTimeout(MAX_RETRIES, MAX_RETRY_TIMEOUT);
  33. asyncClient.setMaxConnections(MAX_CONNECTIONS);
  34. asyncClient.setTimeout(MAX_TIMEOUT);
  35. syncClient.setMaxRetriesAndTimeout(MAX_RETRIES, MAX_RETRY_TIMEOUT);
  36. syncClient.setMaxConnections(MAX_CONNECTIONS);
  37. syncClient.setTimeout(MAX_TIMEOUT);
  38. }
  39. // [+] basic cancel method
  40. public static void cancelAsyncRequest(Context context, boolean mayInterruptIfRunning) {
  41. asyncClient.cancelRequests(context, mayInterruptIfRunning);
  42. }
  43. public static void cancelSyncRequest(Context context, boolean mayInterruptIfRunning) {
  44. syncClient.cancelRequests(context, mayInterruptIfRunning);
  45. }
  46. public static AsyncHttpClient getAsyncClient() {
  47. return asyncClient;
  48. }
  49. public static SyncHttpClient getSyncClient() {
  50. return syncClient;
  51. }
  52. // [+] base get
  53. public static void get(boolean async, String url, AsyncHttpResponseHandler responseHandler) {
  54. get(async, null, url, null, responseHandler);
  55. }
  56. public static void get(boolean async, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
  57. get(async, null, url, params, responseHandler);
  58. }
  59. public static void get(Context context, String url, AsyncHttpResponseHandler responseHandler) {
  60. get(false, context, url, responseHandler);
  61. }
  62. public static void get(boolean async, Context context, String url, AsyncHttpResponseHandler responseHandler) {
  63. get(async, context, url, null, responseHandler);
  64. }
  65. public static void get(boolean async, Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
  66. if (async) {
  67. asyncClient.get(context, url, params, responseHandler);
  68. } else {
  69. syncClient.get(context, url, params, responseHandler);
  70. }
  71. }
  72. // [-] base get
  73. // [+] base post
  74. public static void post(boolean async, String url, AsyncHttpResponseHandler responseHandler) {
  75. post(async, null, url, null, responseHandler);
  76. }
  77. public static void post(boolean async, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
  78. post(async, null, url, params, responseHandler);
  79. }
  80. public static void post(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
  81. post(false, context, url, params, responseHandler);
  82. }
  83. public static void post(boolean async, Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
  84. if (async)
  85. asyncClient.post(context, url, params, responseHandler);
  86. else
  87. syncClient.post(context, url, params, responseHandler);
  88. }
  89. // [-] base post
  90. // [+] post entity
  91. public static void postJSON(Context context, String url, JSON json, AsyncHttpResponseHandler responseHandler) {
  92. postJSON(false, context, url, json, responseHandler);
  93. }
  94. public static void postJSON(boolean async, Context context, String url, JSON json, AsyncHttpResponseHandler responseHandler) {
  95. ByteArrayEntity entity = new ByteArrayEntity(json.toString().getBytes());
  96. postEntity(async, context, url, entity, CONTENT_TYPE_JSON, responseHandler);
  97. }
  98. public static void postJSON(boolean async, Context context, String url, byte[] json, AsyncHttpResponseHandler responseHandler) {
  99. ByteArrayEntity entity = new ByteArrayEntity(json);
  100. postEntity(async, context, url, entity, CONTENT_TYPE_JSON, responseHandler);
  101. }
  102. public static void postJSON(boolean async, String url, JSON json, AsyncHttpResponseHandler responseHandler) {
  103. postJSON(async, null, url, json, responseHandler);
  104. }
  105. public static void postEntity(boolean async, String url, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler) {
  106. postEntity(async, null, url, entity, contentType, responseHandler);
  107. }
  108. public static void postEntity(boolean async, Context context, String url, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler) {
  109. if (async)
  110. asyncClient.post(context, url, entity, contentType, responseHandler);
  111. else
  112. syncClient.post(context, url, entity, contentType, responseHandler);
  113. }
  114. // [-] post entity
  115. // [+] base put
  116. public static void put(boolean async, String url, AsyncHttpResponseHandler responseHandler) {
  117. put(async, null, url, null, responseHandler);
  118. }
  119. public static void put(boolean async, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
  120. put(async, null, url, params, responseHandler);
  121. }
  122. public static void put(Context context, String url, AsyncHttpResponseHandler responseHandler) {
  123. put(false, context, url, responseHandler);
  124. }
  125. public static void put(boolean async, Context context, String url, AsyncHttpResponseHandler responseHandler) {
  126. put(async, context, url, null, responseHandler);
  127. }
  128. public static void put(boolean async, Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
  129. if (async) {
  130. asyncClient.put(context, url, params, responseHandler);
  131. } else {
  132. syncClient.put(context, url, params, responseHandler);
  133. }
  134. }
  135. // [-] base put
  136. // [+] base delete
  137. public static void delete(boolean async, String url, AsyncHttpResponseHandler responseHandler) {
  138. delete(async, null, url, null, responseHandler);
  139. }
  140. public static void delete(boolean async, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
  141. delete(async, null, url, params, responseHandler);
  142. }
  143. public static void delete(Context context, String url, AsyncHttpResponseHandler responseHandler) {
  144. delete(false, context, url, responseHandler);
  145. }
  146. public static void delete(boolean async, Context context, String url, AsyncHttpResponseHandler responseHandler) {
  147. delete(async, context, url, null, responseHandler);
  148. }
  149. public static void delete(boolean async, Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
  150. if (async) {
  151. asyncClient.delete(context, url, null, params, responseHandler);
  152. } else {
  153. syncClient.delete(context, url, null, params, responseHandler);
  154. }
  155. }
  156. // [-] base put
  157. }