OkHttpUtils.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package com.zhy.http.okhttp;
  2. import com.zhy.http.okhttp.builder.GetBuilder;
  3. import com.zhy.http.okhttp.builder.HeadBuilder;
  4. import com.zhy.http.okhttp.builder.OtherRequestBuilder;
  5. import com.zhy.http.okhttp.builder.PostFileBuilder;
  6. import com.zhy.http.okhttp.builder.PostFormBuilder;
  7. import com.zhy.http.okhttp.builder.PostStringBuilder;
  8. import com.zhy.http.okhttp.callback.Callback;
  9. import com.zhy.http.okhttp.request.RequestCall;
  10. import com.zhy.http.okhttp.utils.Platform;
  11. import java.io.IOException;
  12. import java.util.concurrent.Executor;
  13. import okhttp3.Call;
  14. import okhttp3.OkHttpClient;
  15. import okhttp3.Response;
  16. /**
  17. * Created by zhy on 15/8/17.
  18. */
  19. public class OkHttpUtils
  20. {
  21. public static final long DEFAULT_MILLISECONDS = 10_000L;
  22. private volatile static OkHttpUtils mInstance;
  23. private OkHttpClient mOkHttpClient;
  24. private Platform mPlatform;
  25. public OkHttpUtils(OkHttpClient okHttpClient)
  26. {
  27. if (okHttpClient == null)
  28. {
  29. mOkHttpClient = new OkHttpClient();
  30. } else
  31. {
  32. mOkHttpClient = okHttpClient;
  33. }
  34. mPlatform = Platform.get();
  35. }
  36. public static OkHttpUtils initClient(OkHttpClient okHttpClient)
  37. {
  38. if (mInstance == null)
  39. {
  40. synchronized (OkHttpUtils.class)
  41. {
  42. if (mInstance == null)
  43. {
  44. mInstance = new OkHttpUtils(okHttpClient);
  45. }
  46. }
  47. }
  48. return mInstance;
  49. }
  50. public static OkHttpUtils getInstance()
  51. {
  52. return initClient(null);
  53. }
  54. public Executor getDelivery()
  55. {
  56. return mPlatform.defaultCallbackExecutor();
  57. }
  58. public OkHttpClient getOkHttpClient()
  59. {
  60. return mOkHttpClient;
  61. }
  62. public static GetBuilder get()
  63. {
  64. return new GetBuilder();
  65. }
  66. public static PostStringBuilder postString()
  67. {
  68. return new PostStringBuilder();
  69. }
  70. public static PostFileBuilder postFile()
  71. {
  72. return new PostFileBuilder();
  73. }
  74. public static PostFormBuilder post()
  75. {
  76. return new PostFormBuilder();
  77. }
  78. public static OtherRequestBuilder put()
  79. {
  80. return new OtherRequestBuilder(METHOD.PUT);
  81. }
  82. public static HeadBuilder head()
  83. {
  84. return new HeadBuilder();
  85. }
  86. public static OtherRequestBuilder delete()
  87. {
  88. return new OtherRequestBuilder(METHOD.DELETE);
  89. }
  90. public static OtherRequestBuilder patch()
  91. {
  92. return new OtherRequestBuilder(METHOD.PATCH);
  93. }
  94. public void execute(final RequestCall requestCall, Callback callback)
  95. {
  96. if (callback == null)
  97. callback = Callback.CALLBACK_DEFAULT;
  98. final Callback finalCallback = callback;
  99. final int id = requestCall.getOkHttpRequest().getId();
  100. requestCall.getCall().enqueue(new okhttp3.Callback()
  101. {
  102. @Override
  103. public void onFailure(Call call, final IOException e)
  104. {
  105. sendFailResultCallback(call, e, finalCallback, id);
  106. }
  107. @Override
  108. public void onResponse(final Call call, final Response response)
  109. {
  110. try
  111. {
  112. if (call.isCanceled())
  113. {
  114. sendFailResultCallback(call, new IOException("Canceled!"), finalCallback, id);
  115. return;
  116. }
  117. if (!finalCallback.validateReponse(response, id))
  118. {
  119. sendFailResultCallback(call, new IOException("request failed , reponse's code is : " + response.code()), finalCallback, id);
  120. return;
  121. }
  122. Object o = finalCallback.parseNetworkResponse(response, id);
  123. sendSuccessResultCallback(o, finalCallback, id);
  124. } catch (Exception e)
  125. {
  126. sendFailResultCallback(call, e, finalCallback, id);
  127. } finally
  128. {
  129. if (response.body() != null)
  130. response.body().close();
  131. }
  132. }
  133. });
  134. }
  135. public void sendFailResultCallback(final Call call, final Exception e, final Callback callback, final int id)
  136. {
  137. if (callback == null) return;
  138. mPlatform.execute(new Runnable()
  139. {
  140. @Override
  141. public void run()
  142. {
  143. callback.onError(call, e, id);
  144. callback.onAfter(id);
  145. }
  146. });
  147. }
  148. public void sendSuccessResultCallback(final Object object, final Callback callback, final int id)
  149. {
  150. if (callback == null) return;
  151. mPlatform.execute(new Runnable()
  152. {
  153. @Override
  154. public void run()
  155. {
  156. callback.onResponse(object, id);
  157. callback.onAfter(id);
  158. }
  159. });
  160. }
  161. public void cancelTag(Object tag)
  162. {
  163. for (Call call : mOkHttpClient.dispatcher().queuedCalls())
  164. {
  165. if (tag.equals(call.request().tag()))
  166. {
  167. call.cancel();
  168. }
  169. }
  170. for (Call call : mOkHttpClient.dispatcher().runningCalls())
  171. {
  172. if (tag.equals(call.request().tag()))
  173. {
  174. call.cancel();
  175. }
  176. }
  177. }
  178. public static class METHOD
  179. {
  180. public static final String HEAD = "HEAD";
  181. public static final String DELETE = "DELETE";
  182. public static final String PUT = "PUT";
  183. public static final String PATCH = "PATCH";
  184. }
  185. }