ObjectCall.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package com.benyanyi.okhttp.call;
  2. import android.content.Context;
  3. import com.benyanyi.okhttp.config.CallType;
  4. import com.benyanyi.okhttp.listener.OnOkHttpListener;
  5. import com.benyanyi.okhttp.util.CacheUtils;
  6. import com.benyanyi.okhttp.util.FormatUtil;
  7. import com.benyanyi.okhttp.util.Internet;
  8. import com.benyanyi.okhttp.util.InternetBean;
  9. import com.benyanyi.okhttp.util.OkHttpLog;
  10. import java.io.IOException;
  11. import io.reactivex.Observable;
  12. import io.reactivex.ObservableEmitter;
  13. import io.reactivex.ObservableOnSubscribe;
  14. import io.reactivex.Observer;
  15. import io.reactivex.android.schedulers.AndroidSchedulers;
  16. import io.reactivex.disposables.Disposable;
  17. import io.reactivex.schedulers.Schedulers;
  18. import okhttp3.Call;
  19. import okhttp3.Callback;
  20. import okhttp3.Response;
  21. import okhttp3.ResponseBody;
  22. /**
  23. * @author YanYi
  24. * @date 2019-08-12 09:43
  25. * @email ben@yanyi.red
  26. * @overview
  27. */
  28. class ObjectCall {
  29. private CallType callType;
  30. private Call call;
  31. private boolean isCache;//是否需要缓存
  32. private String mCacheName;//缓存名称
  33. private Context context;
  34. private OnOkHttpListener<Object> onOkHttpListener;
  35. ObjectCall(CallBuilder builder) {
  36. this.callType = builder.getCallType();
  37. this.call = builder.getCall();
  38. this.isCache = builder.isCache();
  39. this.mCacheName = builder.getCacheName();
  40. this.context = builder.getContext();
  41. }
  42. void send(OnOkHttpListener<Object> okHttpListener) {
  43. this.onOkHttpListener = okHttpListener;
  44. InternetBean internetBean = Internet.ifInternet(context);
  45. if (internetBean.getStatus()) {
  46. sendCall().subscribeOn(Schedulers.io())
  47. .observeOn(AndroidSchedulers.mainThread())
  48. .serialize()
  49. .subscribe(new Observer<Object>() {
  50. @Override
  51. public void onSubscribe(Disposable d) {
  52. }
  53. @Override
  54. public void onNext(Object o) {
  55. onOkHttpListener.onSuccess(o);
  56. }
  57. @Override
  58. public void onError(Throwable e) {
  59. onOkHttpListener.onFailure(e);
  60. }
  61. @Override
  62. public void onComplete() {
  63. onOkHttpListener.onCompleted();
  64. }
  65. });
  66. } else {
  67. if (isCache) {
  68. String str = CacheUtils.getInstance(context).getCacheToLocalJson(mCacheName);
  69. onOkHttpListener.onSuccess(str);
  70. }
  71. onOkHttpListener.onFailure(new Exception(internetBean.getMsg()));
  72. onOkHttpListener.onCompleted();
  73. }
  74. }
  75. private Observable<Object> sendCall() {
  76. return Observable.create(new ObservableOnSubscribe<Object>() {
  77. @Override
  78. public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
  79. send(emitter);
  80. }
  81. });
  82. }
  83. private void send(ObservableEmitter<Object> emitter) {
  84. if (callType == CallType.ASYNC) {
  85. async(emitter);
  86. } else {
  87. sync(emitter);
  88. }
  89. }
  90. private void async(final ObservableEmitter<Object> emitter) {
  91. call.enqueue(new Callback() {
  92. @Override
  93. public void onFailure(Call call, IOException e) {
  94. OkHttpLog.e(e.getMessage());
  95. emitter.onError(e);
  96. emitter.onComplete();
  97. }
  98. @Override
  99. public void onResponse(Call call, Response response) throws IOException {
  100. success(response, emitter);
  101. }
  102. });
  103. }
  104. private void sync(ObservableEmitter<Object> emitter) {
  105. try {
  106. Response response = call.execute();
  107. success(response, emitter);
  108. } catch (IOException e) {
  109. OkHttpLog.e(e.getMessage());
  110. emitter.onError(e);
  111. emitter.onComplete();
  112. }
  113. }
  114. private void success(Response response, ObservableEmitter<Object> emitter) throws IOException {
  115. ResponseBody body = response.body();
  116. if (body != null) {
  117. if (response.isSuccessful()) {
  118. if (response.networkResponse() != null) {//网络请求有数据
  119. String str = body.string();
  120. emitter.onNext(dataProcessing(str));
  121. emitter.onComplete();
  122. } else {
  123. dataReturn(body.string(), emitter);
  124. }
  125. } else {
  126. dataReturn(body.string(), emitter);
  127. }
  128. } else {
  129. dataReturn(null, emitter);
  130. }
  131. }
  132. /**
  133. * 数据处理
  134. */
  135. private String dataProcessing(String message) {
  136. String str = message;
  137. OkHttpLog.d(str);
  138. String html = "<!DOCTYPE HTML>";
  139. if (!str.toUpperCase().contains(html)) {
  140. if (FormatUtil.isNotEmpty(mCacheName)) {
  141. CacheUtils.getInstance(context).setCacheToLocalJson(mCacheName, str);
  142. }
  143. } else {
  144. if (isCache) {
  145. str = CacheUtils.getInstance(context).getCacheToLocalJson(mCacheName);
  146. }
  147. }
  148. return str;
  149. }
  150. /**
  151. * 数据返回
  152. *
  153. * @param message
  154. * @param emitter
  155. */
  156. private void dataReturn(String message, ObservableEmitter<Object> emitter) {
  157. if (isCache) {
  158. emitter.onNext(CacheUtils.getInstance(context).getCacheToLocalJson(mCacheName));
  159. emitter.onComplete();
  160. } else {
  161. emitter.onNext(dataProcessing(message));
  162. emitter.onComplete();
  163. }
  164. }
  165. }