Commit 7735a21a48198e2353522b1787481ec587e94f40

Authored by YangShuai
1 parent 6eb51e61

修改网络请求框架

app/strike/src/main/java/com/cnlive/strike/MainActivity.java
@@ -2,27 +2,11 @@ package com.cnlive.strike; @@ -2,27 +2,11 @@ package com.cnlive.strike;
2 2
3 3
4 import android.content.Intent; 4 import android.content.Intent;
5 -import android.os.Handler;  
6 -import android.util.Log;  
7 import android.view.View; 5 import android.view.View;
8 6
9 import com.cnlive.core.libs.base.network.manager.SubscriptionManager; 7 import com.cnlive.core.libs.base.network.manager.SubscriptionManager;
10 -import com.cnlive.core.network.request.BaseRequest;  
11 -import com.cnlive.core.libs.base.network.BaseResult;  
12 -import com.cnlive.core.libs.base.network.HttpConn;  
13 -import com.cnlive.core.libs.base.network.NetCallBack;  
14 -import com.cnlive.strike.network.api.ApiService;  
15 -import com.cnlive.strike.network.bean.LetHimNoSeeBean;  
16 -import com.cnlive.strike.network.bean.TestBean;  
17 import com.cnlive.core.libs.base.frame.activity.BaseActivity; 8 import com.cnlive.core.libs.base.frame.activity.BaseActivity;
18 9
19 -import java.util.HashMap;  
20 -import java.util.List;  
21 -import java.util.Map;  
22 -  
23 -import io.reactivex.ObservableSource;  
24 -import io.reactivex.functions.Function;  
25 -  
26 public class MainActivity extends BaseActivity { 10 public class MainActivity extends BaseActivity {
27 11
28 12
app/strike/src/main/java/com/cnlive/strike/MyFragment.java
1 package com.cnlive.strike; 1 package com.cnlive.strike;
2 2
3 3
4 -import android.content.Intent;  
5 import android.os.Bundle; 4 import android.os.Bundle;
6 -import android.os.Handler;  
7 import android.util.Log; 5 import android.util.Log;
8 import android.view.LayoutInflater; 6 import android.view.LayoutInflater;
9 import android.view.View; 7 import android.view.View;
@@ -15,7 +13,8 @@ import com.cnlive.core.libs.base.network.BaseResult; @@ -15,7 +13,8 @@ import com.cnlive.core.libs.base.network.BaseResult;
15 import com.cnlive.core.libs.base.network.HttpConn; 13 import com.cnlive.core.libs.base.network.HttpConn;
16 import com.cnlive.core.libs.base.network.NetCallBack; 14 import com.cnlive.core.libs.base.network.NetCallBack;
17 import com.cnlive.core.libs.base.util.StatusBarConfig; 15 import com.cnlive.core.libs.base.util.StatusBarConfig;
18 -import com.cnlive.core.network.request.BaseRequest; 16 +import com.cnlive.core.network.interceptor.BasicParamsInterceptor;
  17 +import com.cnlive.strike.network.BaseRequest;
19 import com.cnlive.strike.network.api.ApiService; 18 import com.cnlive.strike.network.api.ApiService;
20 import com.cnlive.strike.network.bean.LetHimNoSeeBean; 19 import com.cnlive.strike.network.bean.LetHimNoSeeBean;
21 import com.cnlive.strike.network.bean.TestBean; 20 import com.cnlive.strike.network.bean.TestBean;
@@ -24,11 +23,9 @@ import androidx.annotation.NonNull; @@ -24,11 +23,9 @@ import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable; 23 import androidx.annotation.Nullable;
25 24
26 import java.util.HashMap; 25 import java.util.HashMap;
27 -import java.util.List;  
28 import java.util.Map; 26 import java.util.Map;
29 -import java.util.concurrent.TimeUnit;  
30 27
31 -import io.reactivex.ObservableSource; 28 +import io.reactivex.Observable;
32 import io.reactivex.functions.Function; 29 import io.reactivex.functions.Function;
33 30
34 31
@@ -54,58 +51,60 @@ public class MyFragment extends BaseFragment<MyFragmentView, MyPresenter> { @@ -54,58 +51,60 @@ public class MyFragment extends BaseFragment<MyFragmentView, MyPresenter> {
54 //单个请求 51 //单个请求
55 HttpConn.action( 52 HttpConn.action(
56 BaseRequest 53 BaseRequest
  54 + .init()
57 .setUrl("http://gank.io/api/data/") 55 .setUrl("http://gank.io/api/data/")
58 .service(ApiService.class) 56 .service(ApiService.class)
59 - .getTest(), new NetCallBack<List<TestBean>>() { 57 + .getTest(), new NetCallBack<TestBean>() {
60 @Override 58 @Override
61 public void onRequestState(int state) { 59 public void onRequestState(int state) {
62 60
63 } 61 }
64 62
65 @Override 63 @Override
66 - public void onSuccess(List<TestBean> data) {  
67 - 64 + public void onSuccess(TestBean data) {
  65 + Log.e(TAG, "onSuccess: " + data.getResults().size());
68 } 66 }
69 67
70 @Override 68 @Override
71 public void onFailure(String errCode, String errMsg) { 69 public void onFailure(String errCode, String errMsg) {
72 - 70 + Log.e(TAG, "onFailure: " + errMsg);
73 } 71 }
74 - });  
75 -  
76 - 72 + }
  73 + );
77 //多个请求 74 //多个请求
78 Map<String, String> param = new HashMap<>(); 75 Map<String, String> param = new HashMap<>();
79 param.put("sid", "358916"); 76 param.put("sid", "358916");
80 - HttpConn.action(  
81 - BaseRequest.service(ApiService.class)  
82 - .getLetHimNoSee(param)  
83 - .flatMap(new Function<BaseResult<LetHimNoSeeBean>, ObservableSource<BaseResult<List<TestBean>>>>() { 77 + HttpConn
  78 + .action(
  79 + BaseRequest
  80 + .init()
  81 + .setUrl("http://gank.io/api/data/")
  82 + .service(ApiService.class)
  83 + .getTest()
  84 + .flatMap(new Function<TestBean, Observable<BaseResult<LetHimNoSeeBean>>>() {
  85 + @Override
  86 + public Observable<BaseResult<LetHimNoSeeBean>> apply(TestBean testBean) throws Exception {
  87 + Log.e(TAG, "apply: " + testBean.getResults().size());
  88 + return BaseRequest.init().service(ApiService.class).getLetHimNoSee(param);
  89 + }
  90 + }), new NetCallBack<BaseResult<LetHimNoSeeBean>>() {
84 @Override 91 @Override
85 - public ObservableSource<BaseResult<List<TestBean>>> apply(BaseResult<LetHimNoSeeBean> letHimNoSeeBeanBaseResult) throws Exception {  
86 - Log.e(TAG, "apply: " + letHimNoSeeBeanBaseResult.getData().getDoNotletFsids().size());  
87 - return BaseRequest.setUrl("http://gank.io/api/data/").service(ApiService.class).getTest(); 92 + public void onRequestState(int state) {
  93 +
88 } 94 }
89 - })  
90 - , new NetCallBack<List<TestBean>>() {  
91 - @Override  
92 - public void onRequestState(int state) {  
93 - Log.e(TAG, "onRequestState: ");  
94 - }  
95 95
96 - @Override  
97 - public void onSuccess(List<TestBean> data) {  
98 - Log.e(TAG, "onSuccess: " + data.size());  
99 - } 96 + @Override
  97 + public void onSuccess(BaseResult<LetHimNoSeeBean> data) {
  98 + Log.e(TAG, "onSuccess: " + data.getData().getDoNotletFsids().size());
  99 + }
  100 +
  101 + @Override
  102 + public void onFailure(String errCode, String errMsg) {
  103 + Log.e(TAG, "onFailure: " + errMsg);
  104 + }
  105 + });
100 106
101 - @Override  
102 - public void onFailure(String errCode, String errMsg) {  
103 - Log.e(TAG, "onFailure: " + errMsg);  
104 -// textView.setText(errMsg);  
105 - getMvpView().setData(errMsg);  
106 107
107 - }  
108 - });  
109 } 108 }
110 }); 109 });
111 } 110 }
core/network/src/main/java/com/cnlive/core/network/request/BaseRequest.java renamed to app/strike/src/main/java/com/cnlive/strike/network/BaseRequest.java
1 -package com.cnlive.core.network.request; 1 +package com.cnlive.strike.network;
2 2
3 3
  4 +import android.text.TextUtils;
  5 +
  6 +import com.cnlive.core.libs.base.application.AppConfig;
  7 +import com.cnlive.core.network.request.NetBuilder;
4 8
5 import java.util.ArrayList; 9 import java.util.ArrayList;
  10 +import java.util.HashMap;
6 import java.util.List; 11 import java.util.List;
  12 +import java.util.Map;
7 13
8 import okhttp3.Interceptor; 14 import okhttp3.Interceptor;
9 -import retrofit2.Retrofit;  
10 15
  16 +/**
  17 + * ys
  18 + */
11 public class BaseRequest { 19 public class BaseRequest {
12 - private static String baseUrl = "http://gank.io/api/data/"; 20 + private String userSetUrl;
13 private static BaseRequest request; 21 private static BaseRequest request;
14 - private static List<Interceptor> interceptorList;  
15 - private Class clzz;  
16 - private static final String DEFAULT_URL = "http://gank.io/api/data/";//默认请求地址  
17 - private static long connectTimeout;//连接超时  
18 - private static long readTimeout;//读取超时  
19 - private static long writeTimeout;//写入超时  
20 - private static final String SP_CMS_URL_DEBUT = "http://cmstest.cnlive.com:8768/";  
21 - private static final String SP_CMS_URL = "http://cms.cnlive.com:8768/";  
22 - 22 + private long connectTimeout;//连接超时
  23 + private long readTimeout;//读取超时
  24 + private long writeTimeout;//写入超时
  25 + private List<Interceptor> interceptorList;//拦截器
  26 + private String baseUrl = "http://gank.io/api/data/";
  27 + private final String SP_CMS_URL_DEBUT = "http://cmstest.cnlive.com:8768/";
  28 + private final String SP_CMS_URL = "http://cms.cnlive.com:8768/";
23 //网++ 接口域名 29 //网++ 接口域名
24 - public static final String SP_BASE_URL = "https://apiwjj.cnlive.com/"; 30 + public final String SP_BASE_URL = "https://apiwjj.cnlive.com/";
25 //网++ 测试环境 接口域名 31 //网++ 测试环境 接口域名
26 - public static final String SP_DEBUG_URL = "http://apiwjjtest.cnlive.com/"; 32 + public final String SP_DEBUG_URL = "http://apiwjjtest.cnlive.com/";
27 //OPEN API 接口域名 33 //OPEN API 接口域名
28 - private static final String SP_OPEN_URL = "https://api.cnlive.com/"; 34 + private final String SP_OPEN_URL = "https://api.cnlive.com/";
  35 +
  36 + /**
  37 + * 构造函数初始化
  38 + */
  39 + public BaseRequest() {
  40 + interceptorList = new ArrayList<>();
  41 + }
29 42
30 43
  44 + /**
  45 + * 设置连接超时
  46 + *
  47 + * @param connectTimeout
  48 + * @return
  49 + */
31 public BaseRequest setConnectTimeout(long connectTimeout) { 50 public BaseRequest setConnectTimeout(long connectTimeout) {
32 init(); 51 init();
33 - this.connectTimeout = connectTimeout; 52 + connectTimeout = connectTimeout;
34 return request; 53 return request;
35 } 54 }
36 55
  56 + /**
  57 + * 设置读取超时
  58 + *
  59 + * @param readTimeout
  60 + * @return
  61 + */
37 public BaseRequest setReadTimeout(long readTimeout) { 62 public BaseRequest setReadTimeout(long readTimeout) {
38 init(); 63 init();
39 - this.readTimeout = readTimeout; 64 + readTimeout = readTimeout;
40 return request; 65 return request;
41 } 66 }
42 67
  68 + /**
  69 + * 设置写入超时
  70 + *
  71 + * @param writeTimeout
  72 + * @return
  73 + */
43 public BaseRequest setWriteTimeout(long writeTimeout) { 74 public BaseRequest setWriteTimeout(long writeTimeout) {
44 init(); 75 init();
45 - this.writeTimeout = writeTimeout; 76 + writeTimeout = writeTimeout;
46 return request; 77 return request;
47 } 78 }
48 79
49 - public BaseRequest() {  
50 - interceptorList = new ArrayList<>();  
51 - initBaseUrl();  
52 - }  
53 80
54 /** 81 /**
55 * 初始化接口地址 82 * 初始化接口地址
56 */ 83 */
57 - private static void initBaseUrl() {  
58 - baseUrl = SP_DEBUG_URL;//还原初始地址 84 + private String initBaseUrl(Class clzz) {
  85 + if (AppConfig.isDebug()) {
  86 + baseUrl = SP_DEBUG_URL;//还原初始地址
  87 + }
  88 + return baseUrl;
59 } 89 }
60 90
61 /** 91 /**
@@ -64,9 +94,9 @@ public class BaseRequest { @@ -64,9 +94,9 @@ public class BaseRequest {
64 * @param url 94 * @param url
65 * @return 95 * @return
66 */ 96 */
67 - public static BaseRequest setUrl(String url) { 97 + public BaseRequest setUrl(String url) {
68 init(); 98 init();
69 - baseUrl = url; 99 + userSetUrl = url;
70 return request; 100 return request;
71 } 101 }
72 102
@@ -76,8 +106,7 @@ public class BaseRequest { @@ -76,8 +106,7 @@ public class BaseRequest {
76 * @param interceptor 106 * @param interceptor
77 * @return 107 * @return
78 */ 108 */
79 - public static BaseRequest addInterceptor(Interceptor interceptor) {  
80 - init(); 109 + public BaseRequest addInterceptor(Interceptor interceptor) {
81 if (null != interceptor) { 110 if (null != interceptor) {
82 interceptorList.add(interceptor); 111 interceptorList.add(interceptor);
83 } 112 }
@@ -87,15 +116,14 @@ public class BaseRequest { @@ -87,15 +116,14 @@ public class BaseRequest {
87 /** 116 /**
88 * @return 117 * @return
89 */ 118 */
90 - private static synchronized BaseRequest init() { 119 + public static synchronized BaseRequest init() {
91 if (null == request) { 120 if (null == request) {
92 request = new BaseRequest(); 121 request = new BaseRequest();
93 } 122 }
94 return request; 123 return request;
95 } 124 }
96 125
97 - public static <NetService> NetService service(Class<NetService> clz) {  
98 - init(); 126 + public <NetService> NetService service(Class<NetService> clz) {
99 NetService service = null; 127 NetService service = null;
100 NetBuilder builder = new NetBuilder(clz); 128 NetBuilder builder = new NetBuilder(clz);
101 if (connectTimeout > 0) { 129 if (connectTimeout > 0) {
@@ -107,15 +135,31 @@ public class BaseRequest { @@ -107,15 +135,31 @@ public class BaseRequest {
107 if (writeTimeout > 0) { 135 if (writeTimeout > 0) {
108 builder.setWriteTimeout(writeTimeout); 136 builder.setWriteTimeout(writeTimeout);
109 } 137 }
110 - if (null!=interceptorList&&interceptorList.size()>0){ 138 + if (null != interceptorList && interceptorList.size() > 0) {
111 builder.addInterceptors(interceptorList); 139 builder.addInterceptors(interceptorList);
112 } 140 }
113 - builder.setBaseUrl(baseUrl); 141 + //设置接口请求头地址
  142 + if (!TextUtils.isEmpty(userSetUrl)) {
  143 + builder.setBaseUrl(userSetUrl);
  144 + userSetUrl = "";
  145 + } else {
  146 + builder.setBaseUrl(initBaseUrl(clz));
  147 + }
114 service = (NetService) builder.getService(); 148 service = (NetService) builder.getService();
115 - initBaseUrl(); 149 + builder = null;
  150 + //重置属性
  151 + resetData();
116 return service; 152 return service;
117 153
118 } 154 }
119 155
  156 + private void resetData() {
  157 + interceptorList.clear();
  158 +
  159 + connectTimeout = 0;
  160 + readTimeout = 0;
  161 + writeTimeout = 0;
  162 + }
  163 +
120 164
121 } 165 }
122 \ No newline at end of file 166 \ No newline at end of file
app/strike/src/main/java/com/cnlive/strike/network/api/ApiService.java
@@ -4,7 +4,6 @@ import com.cnlive.core.libs.base.network.BaseResult; @@ -4,7 +4,6 @@ import com.cnlive.core.libs.base.network.BaseResult;
4 import com.cnlive.strike.network.bean.LetHimNoSeeBean; 4 import com.cnlive.strike.network.bean.LetHimNoSeeBean;
5 import com.cnlive.strike.network.bean.TestBean; 5 import com.cnlive.strike.network.bean.TestBean;
6 6
7 -import java.util.List;  
8 import java.util.Map; 7 import java.util.Map;
9 8
10 import io.reactivex.Observable; 9 import io.reactivex.Observable;
@@ -14,7 +13,7 @@ import retrofit2.http.QueryMap; @@ -14,7 +13,7 @@ import retrofit2.http.QueryMap;
14 13
15 public interface ApiService { 14 public interface ApiService {
16 @GET("福利/10/1") 15 @GET("福利/10/1")
17 - Observable<BaseResult<List<TestBean>>> getTest(); 16 + Observable<TestBean> getTest();
18 17
19 @POST("Daren/moment/getDoNotletFsids.action") 18 @POST("Daren/moment/getDoNotletFsids.action")
20 Observable<BaseResult<LetHimNoSeeBean>> getLetHimNoSee(@QueryMap() Map<String, String> maps); 19 Observable<BaseResult<LetHimNoSeeBean>> getLetHimNoSee(@QueryMap() Map<String, String> maps);
app/strike/src/main/java/com/cnlive/strike/network/bean/TestBean.java
1 package com.cnlive.strike.network.bean; 1 package com.cnlive.strike.network.bean;
2 2
3 -public class TestBean { 3 +import java.util.List;
4 4
  5 +public class TestBean {
5 /** 6 /**
6 - * _id : 5ccdbc219d212239df927a93  
7 - * createdAt : 2019-05-04T16:21:53.523Z  
8 - * desc : 2019-05-05  
9 - * publishedAt : 2019-05-04T16:21:59.733Z  
10 - * source : web  
11 - * type : 福利  
12 - * url : http://ww1.sinaimg.cn/large/0065oQSqly1g2pquqlp0nj30n00yiq8u.jpg  
13 - * used : true  
14 - * who : lijinshanmx 7 + * error : false
  8 + * results : [{"_id":"5ccdbc219d212239df927a93","createdAt":"2019-05-04T16:21:53.523Z","desc":"2019-05-05","publishedAt":"2019-05-04T16:21:59.733Z","source":"web","type":"福利","url":"http://ww1.sinaimg.cn/large/0065oQSqly1g2pquqlp0nj30n00yiq8u.jpg","used":true,"who":"lijinshanmx"},{"_id":"5cc43919fc3326376038d233","createdAt":"2019-04-27T19:12:25.536Z","desc":"2019-04-27","publishedAt":"2019-04-27T19:12:51.865Z","source":"web","type":"福利","url":"https://ww1.sinaimg.cn/large/0065oQSqly1g2hekfwnd7j30sg0x4djy.jpg","used":true,"who":"lijinshanmx"},{"_id":"5c6a4ae99d212226776d3256","createdAt":"2019-02-18T06:04:25.571Z","desc":"2019-02-18","publishedAt":"2019-04-10T00:00:00.0Z","source":"web","type":"福利","url":"https://ws1.sinaimg.cn/large/0065oQSqly1g0ajj4h6ndj30sg11xdmj.jpg","used":true,"who":"lijinshanmx"},{"_id":"5c2dabdb9d21226e068debf9","createdAt":"2019-01-03T06:29:47.895Z","desc":"2019-01-03","publishedAt":"2019-01-03T00:00:00.0Z","source":"web","type":"福利","url":"https://ws1.sinaimg.cn/large/0065oQSqly1fytdr77urlj30sg10najf.jpg","used":true,"who":"lijinshanmx"},{"_id":"5c25db189d21221e8ada8664","createdAt":"2018-12-28T08:13:12.688Z","desc":"2018-12-28","publishedAt":"2018-12-28T00:00:00.0Z","source":"web","type":"福利","url":"https://ws1.sinaimg.cn/large/0065oQSqly1fymj13tnjmj30r60zf79k.jpg","used":true,"who":"lijinshanmx"},{"_id":"5c12216d9d21223f5a2baea2","createdAt":"2018-12-13T09:07:57.2Z","desc":"2018-12-13","publishedAt":"2018-12-13T00:00:00.0Z","source":"web","type":"福利","url":"https://ws1.sinaimg.cn/large/0065oQSqgy1fy58bi1wlgj30sg10hguu.jpg","used":true,"who":"lijinshanmx"},{"_id":"5bfe1a5b9d2122309624cbb7","createdAt":"2018-11-28T04:32:27.338Z","desc":"2018-11-28","publishedAt":"2018-11-28T00:00:00.0Z","source":"web","type":"福利","url":"https://ws1.sinaimg.cn/large/0065oQSqgy1fxno2dvxusj30sf10nqcm.jpg","used":true,"who":"lijinshanmx"},{"_id":"5bf22fd69d21223ddba8ca25","createdAt":"2018-11-19T03:36:54.950Z","desc":"2018-11-19","publishedAt":"2018-11-19T00:00:00.0Z","source":"web","type":"福利","url":"https://ws1.sinaimg.cn/large/0065oQSqgy1fxd7vcz86nj30qo0ybqc1.jpg","used":true,"who":"lijinshanmx"},{"_id":"5be14edb9d21223dd50660f8","createdAt":"2018-11-06T08:20:43.656Z","desc":"2018-11-06","publishedAt":"2018-11-06T00:00:00.0Z","source":"web","type":"福利","url":"https://ws1.sinaimg.cn/large/0065oQSqgy1fwyf0wr8hhj30ie0nhq6p.jpg","used":true,"who":"lijinshanmx"},{"_id":"5bcd71979d21220315c663fc","createdAt":"2018-10-22T06:43:35.440Z","desc":"2018-10-22","publishedAt":"2018-10-22T00:00:00.0Z","source":"web","type":"福利","url":"https://ws1.sinaimg.cn/large/0065oQSqgy1fwgzx8n1syj30sg15h7ew.jpg","used":true,"who":"lijinshanmx"}]
15 */ 9 */
16 10
17 - private String _id;  
18 - private String createdAt;  
19 - private String desc;  
20 - private String publishedAt;  
21 - private String source;  
22 - private String type;  
23 - private String url;  
24 - private boolean used;  
25 - private String who;  
26 -  
27 - public String get_id() {  
28 - return _id;  
29 - }  
30 -  
31 - public void set_id(String _id) {  
32 - this._id = _id;  
33 - }  
34 -  
35 - public String getCreatedAt() {  
36 - return createdAt;  
37 - }  
38 -  
39 - public void setCreatedAt(String createdAt) {  
40 - this.createdAt = createdAt;  
41 - }  
42 -  
43 - public String getDesc() {  
44 - return desc;  
45 - }  
46 -  
47 - public void setDesc(String desc) {  
48 - this.desc = desc;  
49 - }  
50 -  
51 - public String getPublishedAt() {  
52 - return publishedAt;  
53 - } 11 + private boolean error;
  12 + private List<ResultsBean> results;
54 13
55 - public void setPublishedAt(String publishedAt) {  
56 - this.publishedAt = publishedAt; 14 + public boolean isError() {
  15 + return error;
57 } 16 }
58 17
59 - public String getSource() {  
60 - return source; 18 + public void setError(boolean error) {
  19 + this.error = error;
61 } 20 }
62 21
63 - public void setSource(String source) {  
64 - this.source = source; 22 + public List<ResultsBean> getResults() {
  23 + return results;
65 } 24 }
66 25
67 - public String getType() {  
68 - return type; 26 + public void setResults(List<ResultsBean> results) {
  27 + this.results = results;
69 } 28 }
70 29
71 - public void setType(String type) {  
72 - this.type = type;  
73 - } 30 + public static class ResultsBean {
  31 + /**
  32 + * _id : 5ccdbc219d212239df927a93
  33 + * createdAt : 2019-05-04T16:21:53.523Z
  34 + * desc : 2019-05-05
  35 + * publishedAt : 2019-05-04T16:21:59.733Z
  36 + * source : web
  37 + * type : 福利
  38 + * url : http://ww1.sinaimg.cn/large/0065oQSqly1g2pquqlp0nj30n00yiq8u.jpg
  39 + * used : true
  40 + * who : lijinshanmx
  41 + */
  42 +
  43 + private String _id;
  44 + private String createdAt;
  45 + private String desc;
  46 + private String publishedAt;
  47 + private String source;
  48 + private String type;
  49 + private String url;
  50 + private boolean used;
  51 + private String who;
  52 +
  53 + public String get_id() {
  54 + return _id;
  55 + }
  56 +
  57 + public void set_id(String _id) {
  58 + this._id = _id;
  59 + }
  60 +
  61 + public String getCreatedAt() {
  62 + return createdAt;
  63 + }
  64 +
  65 + public void setCreatedAt(String createdAt) {
  66 + this.createdAt = createdAt;
  67 + }
  68 +
  69 + public String getDesc() {
  70 + return desc;
  71 + }
  72 +
  73 + public void setDesc(String desc) {
  74 + this.desc = desc;
  75 + }
  76 +
  77 + public String getPublishedAt() {
  78 + return publishedAt;
  79 + }
  80 +
  81 + public void setPublishedAt(String publishedAt) {
  82 + this.publishedAt = publishedAt;
  83 + }
  84 +
  85 + public String getSource() {
  86 + return source;
  87 + }
  88 +
  89 + public void setSource(String source) {
  90 + this.source = source;
  91 + }
  92 +
  93 + public String getType() {
  94 + return type;
  95 + }
  96 +
  97 + public void setType(String type) {
  98 + this.type = type;
  99 + }
  100 +
  101 + public String getUrl() {
  102 + return url;
  103 + }
  104 +
  105 + public void setUrl(String url) {
  106 + this.url = url;
  107 + }
  108 +
  109 + public boolean isUsed() {
  110 + return used;
  111 + }
  112 +
  113 + public void setUsed(boolean used) {
  114 + this.used = used;
  115 + }
  116 +
  117 + public String getWho() {
  118 + return who;
  119 + }
  120 +
  121 + public void setWho(String who) {
  122 + this.who = who;
  123 + }
  124 + }
  125 +
  126 +// /**
  127 +// * _id : 5ccdbc219d212239df927a93
  128 +// * createdAt : 2019-05-04T16:21:53.523Z
  129 +// * desc : 2019-05-05
  130 +// * publishedAt : 2019-05-04T16:21:59.733Z
  131 +// * source : web
  132 +// * type : 福利
  133 +// * url : http://ww1.sinaimg.cn/large/0065oQSqly1g2pquqlp0nj30n00yiq8u.jpg
  134 +// * used : true
  135 +// * who : lijinshanmx
  136 +// */
  137 +//
  138 +// private String _id;
  139 +// private String createdAt;
  140 +// private String desc;
  141 +// private String publishedAt;
  142 +// private String source;
  143 +// private String type;
  144 +// private String url;
  145 +// private boolean used;
  146 +// private String who;
  147 +//
  148 +// public String get_id() {
  149 +// return _id;
  150 +// }
  151 +//
  152 +// public void set_id(String _id) {
  153 +// this._id = _id;
  154 +// }
  155 +//
  156 +// public String getCreatedAt() {
  157 +// return createdAt;
  158 +// }
  159 +//
  160 +// public void setCreatedAt(String createdAt) {
  161 +// this.createdAt = createdAt;
  162 +// }
  163 +//
  164 +// public String getDesc() {
  165 +// return desc;
  166 +// }
  167 +//
  168 +// public void setDesc(String desc) {
  169 +// this.desc = desc;
  170 +// }
  171 +//
  172 +// public String getPublishedAt() {
  173 +// return publishedAt;
  174 +// }
  175 +//
  176 +// public void setPublishedAt(String publishedAt) {
  177 +// this.publishedAt = publishedAt;
  178 +// }
  179 +//
  180 +// public String getSource() {
  181 +// return source;
  182 +// }
  183 +//
  184 +// public void setSource(String source) {
  185 +// this.source = source;
  186 +// }
  187 +//
  188 +// public String getType() {
  189 +// return type;
  190 +// }
  191 +//
  192 +// public void setType(String type) {
  193 +// this.type = type;
  194 +// }
  195 +//
  196 +// public String getUrl() {
  197 +// return url;
  198 +// }
  199 +//
  200 +// public void setUrl(String url) {
  201 +// this.url = url;
  202 +// }
  203 +//
  204 +// public boolean isUsed() {
  205 +// return used;
  206 +// }
  207 +//
  208 +// public void setUsed(boolean used) {
  209 +// this.used = used;
  210 +// }
  211 +//
  212 +// public String getWho() {
  213 +// return who;
  214 +// }
  215 +//
  216 +// public void setWho(String who) {
  217 +// this.who = who;
  218 +// }
74 219
75 - public String getUrl() {  
76 - return url;  
77 - }  
78 220
79 - public void setUrl(String url) {  
80 - this.url = url;  
81 - }  
82 -  
83 - public boolean isUsed() {  
84 - return used;  
85 - }  
86 -  
87 - public void setUsed(boolean used) {  
88 - this.used = used;  
89 - }  
90 -  
91 - public String getWho() {  
92 - return who;  
93 - }  
94 -  
95 - public void setWho(String who) {  
96 - this.who = who;  
97 - }  
98 } 221 }
core/network/src/main/java/com/cnlive/core/network/request/BaseObserver.java renamed to core/base/src/main/java/com/cnlive/core/libs/base/network/BaseObserver.java
1 -package com.cnlive.core.network.request; 1 +package com.cnlive.core.libs.base.network;
2 2
3 import android.accounts.NetworkErrorException; 3 import android.accounts.NetworkErrorException;
4 import android.content.Context; 4 import android.content.Context;
@@ -13,12 +13,14 @@ import io.reactivex.Observer; @@ -13,12 +13,14 @@ import io.reactivex.Observer;
13 import io.reactivex.disposables.Disposable; 13 import io.reactivex.disposables.Disposable;
14 14
15 /** 15 /**
  16 + * ys
16 * Observer基类 17 * Observer基类
17 * 18 *
18 * @param <T> 19 * @param <T>
19 */ 20 */
20 -public abstract class BaseObserver<T> implements Observer<BaseResult<T>> { 21 +public abstract class BaseObserver<T> implements Observer<T> {
21 protected Context context; 22 protected Context context;
  23 + private static final String EXCEPTION_CODE = "-1";//异常
22 24
23 public BaseObserver() { 25 public BaseObserver() {
24 } 26 }
@@ -29,24 +31,34 @@ public abstract class BaseObserver&lt;T&gt; implements Observer&lt;BaseResult&lt;T&gt;&gt; { @@ -29,24 +31,34 @@ public abstract class BaseObserver&lt;T&gt; implements Observer&lt;BaseResult&lt;T&gt;&gt; {
29 31
30 @Override 32 @Override
31 public void onSubscribe(Disposable d) { 33 public void onSubscribe(Disposable d) {
32 - onRequestStart(); 34 + onRequestStart(d);
33 } 35 }
34 36
35 @Override 37 @Override
36 - public void onNext(BaseResult<T> tBaseResult) { 38 + public void onNext(T tBaseResult) {
37 //请求结束 39 //请求结束
38 onRequestEnd(); 40 onRequestEnd();
39 - //判断是否请求成功  
40 - if (tBaseResult.isSuccess()) {  
41 - try {  
42 - onSuccess(tBaseResult);  
43 - } catch (Exception e) {  
44 - e.printStackTrace(); 41 + //判断是否请求成功(是网家家数据)
  42 + if (tBaseResult instanceof BaseResult) {
  43 + BaseResult baseResult = (BaseResult) tBaseResult;
  44 + if (baseResult.isSuccess()) {
  45 + try {
  46 + onSuccess(tBaseResult);
  47 + } catch (Exception e) {
  48 + e.printStackTrace();
  49 + }
  50 + } else {
  51 + //状态码不对
  52 + try {
  53 + onCodeError(baseResult.getErrorCode(), baseResult.getErrorMessage());
  54 + } catch (Exception e) {
  55 + e.printStackTrace();
  56 + }
45 } 57 }
46 } else { 58 } else {
47 - //状态码不对 59 + //直接返回成功,非网家家数据
48 try { 60 try {
49 - onCodeError(tBaseResult.getErrorCode(), tBaseResult.getErrorMessage()); 61 + onSuccess(tBaseResult);
50 } catch (Exception e) { 62 } catch (Exception e) {
51 e.printStackTrace(); 63 e.printStackTrace();
52 } 64 }
@@ -57,23 +69,29 @@ public abstract class BaseObserver&lt;T&gt; implements Observer&lt;BaseResult&lt;T&gt;&gt; { @@ -57,23 +69,29 @@ public abstract class BaseObserver&lt;T&gt; implements Observer&lt;BaseResult&lt;T&gt;&gt; {
57 public void onError(Throwable e) { 69 public void onError(Throwable e) {
58 //请求结束 70 //请求结束
59 onRequestEnd(); 71 onRequestEnd();
  72 +// try {
  73 +// //判断是否是网络请求相关错误
  74 +// if (e instanceof ConnectException
  75 +// || e instanceof TimeoutException
  76 +// || e instanceof NetworkErrorException
  77 +// || e instanceof UnknownHostException) {
  78 +// onFailure(e, true);
  79 +// } else {
  80 +// onFailure(e, false);
  81 +// }
  82 +// } catch (Exception e1) {
  83 +// e1.printStackTrace();
  84 +// try {
  85 +// onFailure(e1, false);
  86 +// } catch (Exception ex) {
  87 +// ex.printStackTrace();
  88 +// }
  89 +// }
  90 +
60 try { 91 try {
61 - //判断是否是网络请求相关错误  
62 - if (e instanceof ConnectException  
63 - || e instanceof TimeoutException  
64 - || e instanceof NetworkErrorException  
65 - || e instanceof UnknownHostException) {  
66 - onFailure(e, true);  
67 - } else {  
68 - onFailure(e, false);  
69 - }  
70 - } catch (Exception e1) {  
71 - e1.printStackTrace();  
72 - try {  
73 - onFailure(e1, false);  
74 - } catch (Exception ex) {  
75 - ex.printStackTrace();  
76 - } 92 + onFailure(EXCEPTION_CODE, e.toString());
  93 + } catch (Exception ex) {
  94 + ex.printStackTrace();
77 } 95 }
78 96
79 } 97 }
@@ -83,7 +101,7 @@ public abstract class BaseObserver&lt;T&gt; implements Observer&lt;BaseResult&lt;T&gt;&gt; { @@ -83,7 +101,7 @@ public abstract class BaseObserver&lt;T&gt; implements Observer&lt;BaseResult&lt;T&gt;&gt; {
83 101
84 } 102 }
85 103
86 - protected void onRequestStart() { 104 + protected void onRequestStart(Disposable d) {
87 } 105 }
88 106
89 protected void onRequestEnd() { 107 protected void onRequestEnd() {
@@ -94,22 +112,22 @@ public abstract class BaseObserver&lt;T&gt; implements Observer&lt;BaseResult&lt;T&gt;&gt; { @@ -94,22 +112,22 @@ public abstract class BaseObserver&lt;T&gt; implements Observer&lt;BaseResult&lt;T&gt;&gt; {
94 * 112 *
95 * @throws Exception 113 * @throws Exception
96 */ 114 */
97 - protected abstract void onSuccess(BaseResult<T> result) throws Exception; 115 + protected abstract void onSuccess(T result) throws Exception;
98 116
99 /** 117 /**
100 * 返回成功了,但是code错误 118 * 返回成功了,但是code错误
101 * 119 *
102 * @throws Exception 120 * @throws Exception
103 */ 121 */
104 - protected abstract void onCodeError(String errorCode, String msg) throws Exception; 122 + protected abstract void onCodeError(String errCode, String errMsg) throws Exception;
105 123
106 /** 124 /**
107 * 返回失败 125 * 返回失败
108 * 126 *
109 - * @param e  
110 - * @param isNetWorkError 是否是网络错误  
111 * @throws Exception 127 * @throws Exception
112 */ 128 */
113 - protected abstract void onFailure(Throwable e, boolean isNetWorkError) throws Exception; 129 +// protected abstract void onFailure(Throwable e, boolean isNetWorkError) throws Exception;
  130 + protected abstract void onFailure(String errCode, String errMsg) throws Exception;
114 131
  132 + ;
115 } 133 }
core/base/src/main/java/com/cnlive/core/libs/base/network/BaseResult.java
@@ -39,49 +39,4 @@ public class BaseResult&lt;T&gt; { @@ -39,49 +39,4 @@ public class BaseResult&lt;T&gt; {
39 this.data = data; 39 this.data = data;
40 } 40 }
41 41
42 -//private static int SUCCESS_CODE=000000;//成功的code  
43 -// private int code;  
44 -// private String message;  
45 -// private T results;  
46 -// private boolean error;  
47 -//  
48 -// public boolean isSuccess(){  
49 -// return getCode()==SUCCESS_CODE;  
50 -// }  
51 -// public int getCode() {  
52 -// return code;  
53 -// }  
54 -//  
55 -// public void setCode(int code) {  
56 -// this.code = code;  
57 -// }  
58 -//  
59 -// public String getMessage() {  
60 -// return message;  
61 -// }  
62 -//  
63 -// public void setMessage(String message) {  
64 -// this.message = message;  
65 -// }  
66 -//  
67 -// public T getResults() {  
68 -// return results;  
69 -// }  
70 -//  
71 -// public void setResults(T results) {  
72 -// this.results = results;  
73 -// }  
74 -//  
75 -// public boolean isError() {  
76 -// return error;  
77 -// }  
78 -//  
79 -// public void setError(boolean error) {  
80 -// this.error = error;  
81 -// }  
82 -//  
83 -// @Override  
84 -// public String toString() {  
85 -// return super.toString();  
86 -// }  
87 } 42 }
core/base/src/main/java/com/cnlive/core/libs/base/network/HttpConn.java
@@ -2,8 +2,12 @@ package com.cnlive.core.libs.base.network; @@ -2,8 +2,12 @@ package com.cnlive.core.libs.base.network;
2 2
3 import android.util.Log; 3 import android.util.Log;
4 4
  5 +import androidx.databinding.BaseObservable;
  6 +
5 import com.cnlive.core.libs.base.network.manager.SubscriptionManager; 7 import com.cnlive.core.libs.base.network.manager.SubscriptionManager;
6 8
  9 +import java.util.List;
  10 +
7 import io.reactivex.Observable; 11 import io.reactivex.Observable;
8 import io.reactivex.android.schedulers.AndroidSchedulers; 12 import io.reactivex.android.schedulers.AndroidSchedulers;
9 import io.reactivex.disposables.Disposable; 13 import io.reactivex.disposables.Disposable;
@@ -25,68 +29,175 @@ public class HttpConn { @@ -25,68 +29,175 @@ public class HttpConn {
25 /** 29 /**
26 * 统一处理单个请求 30 * 统一处理单个请求
27 * 31 *
  32 + * @param <T>
28 * @param observable 33 * @param observable
29 * @param callBack 34 * @param callBack
30 - * @param <T>  
31 */ 35 */
32 - public static <T> Disposable action(Observable<BaseResult<T>> observable, final NetCallBack<T> callBack) {  
33 - Disposable disposable = null;  
34 - disposable = observable 36 + private static Disposable disposable = null;
  37 +
  38 + public static <T> Disposable action(Observable<T> observable, final NetCallBack<T> callBack) {
  39 + disposable = null;
  40 + observable
35 .subscribeOn(Schedulers.io()) 41 .subscribeOn(Schedulers.io())
36 .observeOn(AndroidSchedulers.mainThread()) 42 .observeOn(AndroidSchedulers.mainThread())
37 - .doOnSubscribe(new Consumer<Disposable>() { 43 + .subscribe(new BaseObserver<T>() {
38 @Override 44 @Override
39 - public void accept(Disposable disposable) throws Exception { 45 + protected void onRequestStart(Disposable d) {
  46 + super.onRequestStart(d);
  47 + disposable = d;
40 if (null != callBack) { 48 if (null != callBack) {
41 callBack.onRequestState(ON_START); 49 callBack.onRequestState(ON_START);
42 } 50 }
43 } 51 }
44 - })  
45 - .doOnNext(new Consumer<BaseResult<T>>() { 52 +
46 @Override 53 @Override
47 - public void accept(BaseResult<T> tBaseResult) throws Exception { 54 + protected void onRequestEnd() {
  55 + super.onRequestEnd();
48 if (null != callBack) { 56 if (null != callBack) {
49 callBack.onRequestState(ON_END); 57 callBack.onRequestState(ON_END);
50 } 58 }
51 } 59 }
52 - })  
53 - .onErrorReturn(new Function<Throwable, BaseResult<T>>() {  
54 @Override 60 @Override
55 - public BaseResult<T> apply(Throwable throwable) {  
56 - Log.e("Sdf", "apply: " + throwable.toString()); 61 + protected void onSuccess(T result) throws Exception {
57 if (null != callBack) { 62 if (null != callBack) {
58 - callBack.onFailure(EXCEPTION_CODE, throwable.toString()); 63 + callBack.onSuccess(result);
59 } 64 }
60 - return null;  
61 } 65 }
62 - })  
63 - .subscribe(new Consumer<BaseResult<T>>() {  
64 @Override 66 @Override
65 - public void accept(BaseResult<T> tBaseResponse) {  
66 - if (tBaseResponse.isSuccess()) {  
67 - if (null != callBack) {  
68 - callBack.onSuccess(tBaseResponse.getData());  
69 - }  
70 - } else {  
71 - if (null != callBack) {  
72 - callBack.onFailure(tBaseResponse.getErrorCode(), tBaseResponse.getErrorMessage());  
73 - } 67 + protected void onCodeError(String errCode, String errMsg) throws Exception {
  68 + if (null != callBack) {
  69 + callBack.onFailure(errCode, errMsg);
74 } 70 }
75 } 71 }
76 - }, new Consumer<Throwable>() {  
77 @Override 72 @Override
78 - public void accept(Throwable throwable) {  
79 -// if (null != callBack) {  
80 -// callBack.onFailure(EXCEPTION_CODE, throwable.getMessage());  
81 -// }  
82 -// Log.e("LinNetError", "单个请求的错误" + throwable.getMessage()); 73 + protected void onFailure(String errCode, String errMsg) throws Exception {
  74 + if (null != callBack) {
  75 + callBack.onFailure(errCode, errMsg);
  76 + }
83 } 77 }
84 }); 78 });
  79 + // .doOnSubscribe(new Consumer<Disposable>() {
  80 + // @Override
  81 + // public void accept(Disposable disposable) throws Exception {
  82 + // if (null != callBack) {
  83 + // callBack.onRequestState(ON_START);
  84 + // }
  85 + // }
  86 + // })
  87 +// .doOnNext(new Consumer<T>() {
  88 +// @Override
  89 +// public void accept(T t) throws Exception {
  90 +// if (null != callBack) {
  91 +// callBack.onRequestState(ON_END);
  92 +// }
  93 +// }
  94 +// })
  95 +// .doOnError(new Consumer<Throwable>() {
  96 +// @Override
  97 +// public void accept(Throwable throwable) throws Exception {
  98 +//
  99 +// }
  100 +// })
  101 +// .onErrorReturn(new Function<Throwable, T>() {
  102 +// @Override
  103 +// public T apply(Throwable throwable) {
  104 +// if (null != callBack) {
  105 +// callBack.onFailure(EXCEPTION_CODE, throwable.toString());
  106 +// }
  107 +// return null;
  108 +// }
  109 +// })
  110 +// .subscribe(new Consumer<T>() {
  111 +// @Override
  112 +// public void accept(T t) {
  113 +// if (t instanceof BaseResult) {
  114 +// BaseResult baseResult = (BaseResult) t;
  115 +// if (baseResult.isSuccess()) {
  116 +// if (null != callBack) {
  117 +// callBack.onSuccess(t);
  118 +// }
  119 +// } else {
  120 +// if (null != callBack) {
  121 +// callBack.onFailure(baseResult.getErrorCode(), baseResult.getErrorMessage());
  122 +// }
  123 +// }
  124 +// } else {
  125 +// if (null != callBack) {
  126 +// callBack.onSuccess(t);
  127 +// }
  128 +// }
  129 +// }
  130 +// });
85 if (null != disposable) { 131 if (null != disposable) {
86 SubscriptionManager.getInstance().add(disposable); 132 SubscriptionManager.getInstance().add(disposable);
87 } 133 }
88 return disposable; 134 return disposable;
89 } 135 }
90 136
  137 + // /**
  138 +// * 返回原数据
  139 +// *
  140 +// * @param observable
  141 +// * @param callBack
  142 +// * @param <T>
  143 +// */
  144 +// public static <T> Disposable action(Observable<BaseResult<T>> observable, final RawNetCallBack<T> callBack) {
  145 +// Disposable disposable = null;
  146 +// disposable = observable
  147 +// .subscribeOn(Schedulers.io())
  148 +// .observeOn(AndroidSchedulers.mainThread())
  149 +// .doOnSubscribe(new Consumer<Disposable>() {
  150 +// @Override
  151 +// public void accept(Disposable disposable) throws Exception {
  152 +// if (null != callBack) {
  153 +// callBack.onRequestState(ON_START);
  154 +// }
  155 +// }
  156 +// })
  157 +// .doOnNext(new Consumer<BaseResult<T>>() {
  158 +// @Override
  159 +// public void accept(BaseResult<T> tBaseResult) throws Exception {
  160 +// if (null != callBack) {
  161 +// callBack.onRequestState(ON_END);
  162 +// }
  163 +// }
  164 +// })
  165 +// .onErrorReturn(new Function<Throwable, BaseResult<T>>() {
  166 +// @Override
  167 +// public BaseResult<T> apply(Throwable throwable) {
  168 +// Log.e("Sdf", "apply: " + throwable.toString());
  169 +// if (null != callBack) {
  170 +// callBack.onFailure(EXCEPTION_CODE, throwable.toString());
  171 +// }
  172 +// return null;
  173 +// }
  174 +// })
  175 +// .subscribe(new Consumer<BaseResult<T>>() {
  176 +// @Override
  177 +// public void accept(BaseResult<T> tBaseResponse) {
  178 +//// if (tBaseResponse.isSuccess()) {
  179 +//// if (null != callBack) {
  180 +//// callBack.onSuccess(tBaseResponse);
  181 +//// }
  182 +//// } else {
  183 +//// if (null != callBack) {
  184 +//// callBack.onFailure(tBaseResponse.getErrorCode(), tBaseResponse.getErrorMessage());
  185 +//// }
  186 +//// }
  187 +// }
  188 +// }, new Consumer<Throwable>() {
  189 +// @Override
  190 +// public void accept(Throwable throwable) {
  191 +//// if (null != callBack) {
  192 +//// callBack.onFailure(EXCEPTION_CODE, throwable.getMessage());
  193 +//// }
  194 +//// Log.e("LinNetError", "单个请求的错误" + throwable.getMessage());
  195 +// }
  196 +// });
  197 +// if (null != disposable) {
  198 +// SubscriptionManager.getInstance().add(disposable);
  199 +// }
  200 +// return disposable;
  201 +// }
91 202
92 } 203 }
core/base/src/main/java/com/cnlive/core/libs/base/network/NetCallBack.java
1 package com.cnlive.core.libs.base.network; 1 package com.cnlive.core.libs.base.network;
2 2
3 -public interface NetCallBack<T> { 3 +public interface NetCallBack<S> {
4 void onRequestState(int state); 4 void onRequestState(int state);
5 5
6 -  
7 /** 6 /**
8 * 数据请求成功 7 * 数据请求成功
9 * 8 *
10 * @param data 请求到的数据 9 * @param data 请求到的数据
11 */ 10 */
12 - void onSuccess(T data); 11 + void onSuccess(S data);
13 12
14 /** 13 /**
15 * 数据请求失败 14 * 数据请求失败
core/base/src/main/java/com/cnlive/core/libs/base/network/RawNetCallBack.java 0 → 100644
  1 +package com.cnlive.core.libs.base.network;
  2 +
  3 +/**
  4 + * 原数据
  5 + *
  6 + * @param <T>
  7 + */
  8 +public interface RawNetCallBack<T> {
  9 + void onRequestState(int state);
  10 +
  11 + /**
  12 + * 数据请求成功
  13 + *
  14 + * @param data 请求到的数据
  15 + */
  16 +// void onSuccess(BaseResult<T> data);
  17 +
  18 + /**
  19 + * 数据请求失败
  20 + */
  21 + void onFailure(String errCode, String errMsg);
  22 +
  23 +}
core/base/src/main/java/com/cnlive/core/libs/base/util/GlideUtil.java
@@ -11,148 +11,145 @@ import android.widget.ImageView; @@ -11,148 +11,145 @@ import android.widget.ImageView;
11 11
12 import com.bumptech.glide.request.RequestOptions; 12 import com.bumptech.glide.request.RequestOptions;
13 import com.bumptech.glide.request.target.ViewTarget; 13 import com.bumptech.glide.request.target.ViewTarget;
14 -import com.cnlive.core.libs.base.application.GlideApp;  
15 -import com.cnlive.core.libs.base.application.GlideRequest;  
16 -import com.cnlive.core.libs.base.application.GlideRequests;  
17 14
18 public class GlideUtil { 15 public class GlideUtil {
19 16
20 - private GlideRequests requests;  
21 - private GlideRequest<Drawable> request;  
22 -  
23 -  
24 - public static ViewTarget<ImageView, Drawable> into(ImageView imageView, String imageUrl) {  
25 - GlideUtil util = with(imageView).load(imageUrl);  
26 - return util.into(imageView);  
27 - }  
28 -  
29 - public static ViewTarget<ImageView, Drawable> into(ImageView imageView, int res) {  
30 - GlideUtil util = with(imageView).load(res);  
31 - return util.into(imageView);  
32 - }  
33 -  
34 - public static ViewTarget<ImageView, Drawable> into(ImageView imageView, Bitmap bitmap) {  
35 - GlideUtil util = with(imageView).load(bitmap);  
36 - return util.into(imageView);  
37 - }  
38 -  
39 - public static ViewTarget<ImageView, Drawable> into(ImageView imageView, Object model, RequestOptions options) {  
40 - GlideUtil util = with(imageView).load(model).apply(options);  
41 - return util.into(imageView);  
42 - }  
43 -  
44 - public static ViewTarget<ImageView, Drawable> into(ImageView imageView, Bitmap bitmap, RequestOptions options) {  
45 - GlideUtil util = with(imageView).load(bitmap).apply(options);  
46 - return util.into(imageView);  
47 - }  
48 -  
49 - public static ViewTarget<ImageView, Drawable> into(ImageView imageView, int res, RequestOptions options) {  
50 - GlideUtil util = with(imageView).load(res).apply(options);  
51 - return util.into(imageView);  
52 - }  
53 -  
54 - public static ViewTarget<ImageView, Drawable> into(ImageView imageView, String imageUrl, RequestOptions options) {  
55 - GlideUtil util = with(imageView).load(imageUrl).apply(options);  
56 - return util.into(imageView);  
57 - }  
58 -  
59 - public static GlideUtil init(ImageView imageView, String imageUrl) {  
60 - return with(imageView).load(imageUrl);  
61 - }  
62 -  
63 - public static GlideUtil init(ImageView imageView, String imageUrl, RequestOptions options) {  
64 - return with(imageView).load(imageUrl).apply(options);  
65 - }  
66 -  
67 - public static GlideUtil init(Context context, String imageUrl) {  
68 - return with(context).load(imageUrl);  
69 - }  
70 -  
71 - public static GlideUtil init(Context context, String imageUrl, RequestOptions options) {  
72 - return with(context).load(imageUrl).apply(options);  
73 - }  
74 -  
75 - public static GlideUtil init(Fragment fragment, String imageUrl) {  
76 - return with(fragment).load(imageUrl);  
77 - }  
78 - public static GlideUtil initGif(Context context, String imageUrl) {  
79 - return with(context).load(imageUrl);  
80 - }  
81 -  
82 - public static GlideUtil init(Fragment fragment, String imageUrl, RequestOptions options) {  
83 - return with(fragment).load(imageUrl).apply(options);  
84 - }  
85 -  
86 -  
87 - private static GlideUtil with(View view) {  
88 - GlideUtil util = new GlideUtil();  
89 - util.requests = GlideApp.with(view);  
90 - return util;  
91 - }  
92 -  
93 - private static GlideUtil with(Context context) {  
94 - GlideUtil util = new GlideUtil();  
95 - util.requests = GlideApp.with(context);  
96 - return util;  
97 - }  
98 -  
99 - private static GlideUtil with(Fragment fragment) {  
100 - GlideUtil util = new GlideUtil();  
101 - util.requests = GlideApp.with(fragment);  
102 - return util;  
103 - }  
104 -  
105 -  
106 - public GlideUtil load(String url) {  
107 - request = requests.load(url);  
108 - return this;  
109 - }  
110 -  
111 - public GlideUtil load(int res) {  
112 - request = requests.load(res);  
113 - return this;  
114 - }  
115 -  
116 - public GlideUtil load(Bitmap bitmap) {  
117 - request = requests.load(bitmap);  
118 - return this;  
119 - }  
120 -  
121 - public GlideUtil load(Object model) {  
122 - request = requests.load(model);  
123 - return this;  
124 - }  
125 -  
126 -  
127 - public GlideUtil apply(RequestOptions options) {  
128 - request = request.apply(options);  
129 - return this;  
130 - }  
131 -  
132 - public GlideUtil error(int arg0) {  
133 - request = request.error(arg0);  
134 - return this;  
135 - }  
136 -  
137 - public GlideUtil error(Drawable arg0) {  
138 - request = request.error(arg0);  
139 - return this;  
140 - }  
141 -  
142 - public GlideUtil circleCrop() {  
143 - request = request.circleCrop();  
144 - return this;  
145 - }  
146 -  
147 - public ViewTarget<ImageView, Drawable> into(ImageView imageView) {  
148 - ViewTarget<ImageView, Drawable> viewTarget = null;  
149 - try {  
150 - viewTarget = request.into(imageView);  
151 - } catch (Exception e) {  
152 - Log.e("GlideUtil", "Error ", e);  
153 - }  
154 -  
155 - return viewTarget;  
156 - } 17 +// private GlideRequests requests;
  18 +// private GlideRequest<Drawable> request;
  19 +//
  20 +//
  21 +// public static ViewTarget<ImageView, Drawable> into(ImageView imageView, String imageUrl) {
  22 +// GlideUtil util = with(imageView).load(imageUrl);
  23 +// return util.into(imageView);
  24 +// }
  25 +//
  26 +// public static ViewTarget<ImageView, Drawable> into(ImageView imageView, int res) {
  27 +// GlideUtil util = with(imageView).load(res);
  28 +// return util.into(imageView);
  29 +// }
  30 +//
  31 +// public static ViewTarget<ImageView, Drawable> into(ImageView imageView, Bitmap bitmap) {
  32 +// GlideUtil util = with(imageView).load(bitmap);
  33 +// return util.into(imageView);
  34 +// }
  35 +//
  36 +// public static ViewTarget<ImageView, Drawable> into(ImageView imageView, Object model, RequestOptions options) {
  37 +// GlideUtil util = with(imageView).load(model).apply(options);
  38 +// return util.into(imageView);
  39 +// }
  40 +//
  41 +// public static ViewTarget<ImageView, Drawable> into(ImageView imageView, Bitmap bitmap, RequestOptions options) {
  42 +// GlideUtil util = with(imageView).load(bitmap).apply(options);
  43 +// return util.into(imageView);
  44 +// }
  45 +//
  46 +// public static ViewTarget<ImageView, Drawable> into(ImageView imageView, int res, RequestOptions options) {
  47 +// GlideUtil util = with(imageView).load(res).apply(options);
  48 +// return util.into(imageView);
  49 +// }
  50 +//
  51 +// public static ViewTarget<ImageView, Drawable> into(ImageView imageView, String imageUrl, RequestOptions options) {
  52 +// GlideUtil util = with(imageView).load(imageUrl).apply(options);
  53 +// return util.into(imageView);
  54 +// }
  55 +//
  56 +// public static GlideUtil init(ImageView imageView, String imageUrl) {
  57 +// return with(imageView).load(imageUrl);
  58 +// }
  59 +//
  60 +// public static GlideUtil init(ImageView imageView, String imageUrl, RequestOptions options) {
  61 +// return with(imageView).load(imageUrl).apply(options);
  62 +// }
  63 +//
  64 +// public static GlideUtil init(Context context, String imageUrl) {
  65 +// return with(context).load(imageUrl);
  66 +// }
  67 +//
  68 +// public static GlideUtil init(Context context, String imageUrl, RequestOptions options) {
  69 +// return with(context).load(imageUrl).apply(options);
  70 +// }
  71 +//
  72 +// public static GlideUtil init(Fragment fragment, String imageUrl) {
  73 +// return with(fragment).load(imageUrl);
  74 +// }
  75 +// public static GlideUtil initGif(Context context, String imageUrl) {
  76 +// return with(context).load(imageUrl);
  77 +// }
  78 +//
  79 +// public static GlideUtil init(Fragment fragment, String imageUrl, RequestOptions options) {
  80 +// return with(fragment).load(imageUrl).apply(options);
  81 +// }
  82 +//
  83 +//
  84 +// private static GlideUtil with(View view) {
  85 +// GlideUtil util = new GlideUtil();
  86 +// util.requests = GlideApp.with(view);
  87 +// return util;
  88 +// }
  89 +//
  90 +// private static GlideUtil with(Context context) {
  91 +// GlideUtil util = new GlideUtil();
  92 +// util.requests = GlideApp.with(context);
  93 +// return util;
  94 +// }
  95 +//
  96 +// private static GlideUtil with(Fragment fragment) {
  97 +// GlideUtil util = new GlideUtil();
  98 +// util.requests = GlideApp.with(fragment);
  99 +// return util;
  100 +// }
  101 +//
  102 +//
  103 +// public GlideUtil load(String url) {
  104 +// request = requests.load(url);
  105 +// return this;
  106 +// }
  107 +//
  108 +// public GlideUtil load(int res) {
  109 +// request = requests.load(res);
  110 +// return this;
  111 +// }
  112 +//
  113 +// public GlideUtil load(Bitmap bitmap) {
  114 +// request = requests.load(bitmap);
  115 +// return this;
  116 +// }
  117 +//
  118 +// public GlideUtil load(Object model) {
  119 +// request = requests.load(model);
  120 +// return this;
  121 +// }
  122 +//
  123 +//
  124 +// public GlideUtil apply(RequestOptions options) {
  125 +// request = request.apply(options);
  126 +// return this;
  127 +// }
  128 +//
  129 +// public GlideUtil error(int arg0) {
  130 +// request = request.error(arg0);
  131 +// return this;
  132 +// }
  133 +//
  134 +// public GlideUtil error(Drawable arg0) {
  135 +// request = request.error(arg0);
  136 +// return this;
  137 +// }
  138 +//
  139 +// public GlideUtil circleCrop() {
  140 +// request = request.circleCrop();
  141 +// return this;
  142 +// }
  143 +//
  144 +// public ViewTarget<ImageView, Drawable> into(ImageView imageView) {
  145 +// ViewTarget<ImageView, Drawable> viewTarget = null;
  146 +// try {
  147 +// viewTarget = request.into(imageView);
  148 +// } catch (Exception e) {
  149 +// Log.e("GlideUtil", "Error ", e);
  150 +// }
  151 +//
  152 +// return viewTarget;
  153 +// }
157 154
158 } 155 }
core/network/src/main/java/com/cnlive/core/network/interceptor/BasicParamsInterceptor.java 0 → 100644
  1 +package com.cnlive.core.network.interceptor;
  2 +
  3 +import android.text.TextUtils;
  4 +
  5 +import java.io.IOException;
  6 +import java.util.ArrayList;
  7 +import java.util.HashMap;
  8 +import java.util.Iterator;
  9 +import java.util.List;
  10 +import java.util.Map;
  11 +
  12 +import okhttp3.FormBody;
  13 +import okhttp3.Headers;
  14 +import okhttp3.HttpUrl;
  15 +import okhttp3.Interceptor;
  16 +import okhttp3.MediaType;
  17 +import okhttp3.Request;
  18 +import okhttp3.RequestBody;
  19 +import okhttp3.Response;
  20 +import okio.Buffer;
  21 +
  22 +public class BasicParamsInterceptor implements Interceptor {
  23 + Map<String, String> queryParamsMap = new HashMap<>(); // 添加到 URL 末尾,Get Post 方法都使用
  24 + Map<String, String> paramsMap = new HashMap<>(); // 添加到公共参数到消息体,适用 Post 请求
  25 + Map<String, String> headerParamsMap = new HashMap<>(); // 公共 Headers 添加
  26 + List<String> headerLinesList = new ArrayList<>(); // 消息头 集合形式,一次添加一行
  27 +
  28 + // 私有构造器
  29 + private BasicParamsInterceptor() {
  30 + }
  31 +
  32 + @Override
  33 + public Response intercept(Chain chain) throws IOException {
  34 +
  35 + Request request = chain.request();
  36 + Request.Builder requestBuilder = request.newBuilder();
  37 +
  38 + // process header params inject
  39 + Headers.Builder headerBuilder = request.headers().newBuilder();
  40 + // 以 Entry 添加消息头
  41 + if (headerParamsMap.size() > 0) {
  42 + Iterator iterator = headerParamsMap.entrySet().iterator();
  43 + while (iterator.hasNext()) {
  44 + Map.Entry entry = (Map.Entry) iterator.next();
  45 + headerBuilder.add((String) entry.getKey(), (String) entry.getValue());
  46 + }
  47 + }
  48 + // 以 String 形式添加消息头
  49 + if (headerLinesList.size() > 0) {
  50 + for (String line : headerLinesList) {
  51 + headerBuilder.add(line);
  52 + }
  53 + requestBuilder.headers(headerBuilder.build());
  54 + }
  55 + // process header params end
  56 +
  57 +
  58 + // process queryParams inject whatever it's GET or POST
  59 + if (queryParamsMap.size() > 0) {
  60 + request = injectParamsIntoUrl(request.url().newBuilder(), requestBuilder, queryParamsMap);
  61 + }
  62 +
  63 + // process post body inject
  64 + if (paramsMap.size() > 0) {
  65 + if (canInjectIntoBody(request)) {
  66 + FormBody.Builder formBodyBuilder = new FormBody.Builder();
  67 + for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
  68 + formBodyBuilder.add((String) entry.getKey(), (String) entry.getValue());
  69 + }
  70 +
  71 + RequestBody formBody = formBodyBuilder.build();
  72 + String postBodyString = bodyToString(request.body());
  73 + postBodyString += ((postBodyString.length() > 0) ? "&" : "") + bodyToString(formBody);
  74 + requestBuilder.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;charset=UTF-8"), postBodyString));
  75 + }
  76 + }
  77 + request = requestBuilder.build();
  78 + return chain.proceed(request);
  79 + }
  80 +
  81 + /**
  82 + * 确认是否是 post 请求
  83 + *
  84 + * @param request 发出的请求
  85 + * @return true 需要注入公共参数
  86 + */
  87 + private boolean canInjectIntoBody(Request request) {
  88 + if (request == null) {
  89 + return false;
  90 + }
  91 + if (!TextUtils.equals(request.method(), "POST")) {
  92 + return false;
  93 + }
  94 + RequestBody body = request.body();
  95 + if (body == null) {
  96 + return false;
  97 + }
  98 + MediaType mediaType = body.contentType();
  99 + if (mediaType == null) {
  100 + return false;
  101 + }
  102 + if (!TextUtils.equals(mediaType.subtype(), "x-www-form-urlencoded")) {
  103 + return false;
  104 + }
  105 + return true;
  106 + }
  107 +
  108 + // func to inject params into url
  109 + private Request injectParamsIntoUrl(HttpUrl.Builder httpUrlBuilder, Request.Builder requestBuilder, Map<String, String> paramsMap) {
  110 + if (paramsMap.size() > 0) {
  111 + Iterator iterator = paramsMap.entrySet().iterator();
  112 + while (iterator.hasNext()) {
  113 + Map.Entry entry = (Map.Entry) iterator.next();
  114 + httpUrlBuilder.addQueryParameter((String) entry.getKey(), (String) entry.getValue());
  115 + }
  116 + requestBuilder.url(httpUrlBuilder.build());
  117 + return requestBuilder.build();
  118 + }
  119 + return null;
  120 + }
  121 +
  122 + private static String bodyToString(final RequestBody request) {
  123 + try {
  124 + final RequestBody copy = request;
  125 + final Buffer buffer = new Buffer();
  126 + if (copy != null)
  127 + copy.writeTo(buffer);
  128 + else
  129 + return "";
  130 + return buffer.readUtf8();
  131 + } catch (final IOException e) {
  132 + return "did not work";
  133 + }
  134 + }
  135 +
  136 + public static class Builder {
  137 +
  138 + BasicParamsInterceptor interceptor;
  139 +
  140 + public Builder() {
  141 + interceptor = new BasicParamsInterceptor();
  142 + }
  143 +
  144 + // 添加公共参数到 post 消息体
  145 + public Builder addParam(String key, String value) {
  146 + interceptor.paramsMap.put(key, value);
  147 + return this;
  148 + }
  149 +
  150 + // 添加公共参数到 post 消息体
  151 + public Builder addParamsMap(Map<String, String> paramsMap) {
  152 + interceptor.paramsMap.putAll(paramsMap);
  153 + return this;
  154 + }
  155 +
  156 + // 添加公共参数到消息头
  157 + public Builder addHeaderParam(String key, String value) {
  158 + interceptor.headerParamsMap.put(key, value);
  159 + return this;
  160 + }
  161 +
  162 + // 添加公共参数到消息头
  163 + public Builder addHeaderParamsMap(Map<String, String> headerParamsMap) {
  164 + interceptor.headerParamsMap.putAll(headerParamsMap);
  165 + return this;
  166 + }
  167 +
  168 + // 添加公共参数到消息头
  169 + public Builder addHeaderLine(String headerLine) {
  170 + int index = headerLine.indexOf(":");
  171 + if (index == -1) {
  172 + throw new IllegalArgumentException("Unexpected header: " + headerLine);
  173 + }
  174 + interceptor.headerLinesList.add(headerLine);
  175 + return this;
  176 + }
  177 +
  178 + // 添加公共参数到消息头
  179 + public Builder addHeaderLinesList(List<String> headerLinesList) {
  180 + for (String headerLine : headerLinesList) {
  181 + int index = headerLine.indexOf(":");
  182 + if (index == -1) {
  183 + throw new IllegalArgumentException("Unexpected header: " + headerLine);
  184 + }
  185 + interceptor.headerLinesList.add(headerLine);
  186 + }
  187 + return this;
  188 + }
  189 +
  190 + // 添加公共参数到 URL
  191 + public Builder addQueryParam(String key, String value) {
  192 + interceptor.queryParamsMap.put(key, value);
  193 + return this;
  194 + }
  195 +
  196 + // 添加公共参数到 URL
  197 + public Builder addQueryParamsMap(Map<String, String> queryParamsMap) {
  198 + interceptor.queryParamsMap.putAll(queryParamsMap);
  199 + return this;
  200 + }
  201 +
  202 + public BasicParamsInterceptor build() {
  203 + return interceptor;
  204 + }
  205 +
  206 + }
  207 +
  208 +}
core/network/src/main/java/com/cnlive/core/network/request/NetBuilder.java
1 package com.cnlive.core.network.request; 1 package com.cnlive.core.network.request;
2 2
3 -import com.cnlive.core.network.converter.GsonConverterFactory;  
4 import com.cnlive.core.network.interceptor.SignEncryptInterceptor; 3 import com.cnlive.core.network.interceptor.SignEncryptInterceptor;
5 4
6 import java.util.ArrayList; 5 import java.util.ArrayList;
  6 +import java.util.HashMap;
7 import java.util.List; 7 import java.util.List;
  8 +import java.util.Map;
8 import java.util.concurrent.TimeUnit; 9 import java.util.concurrent.TimeUnit;
9 10
10 import okhttp3.Interceptor; 11 import okhttp3.Interceptor;
11 import okhttp3.OkHttpClient; 12 import okhttp3.OkHttpClient;
12 import retrofit2.Retrofit; 13 import retrofit2.Retrofit;
13 import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; 14 import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
  15 +import retrofit2.converter.gson.GsonConverterFactory;
14 16
15 public class NetBuilder<NetService> { 17 public class NetBuilder<NetService> {
16 private Class<NetService> netServiceClass;//网络接口类 18 private Class<NetService> netServiceClass;//网络接口类
@@ -36,6 +38,18 @@ public class NetBuilder&lt;NetService&gt; { @@ -36,6 +38,18 @@ public class NetBuilder&lt;NetService&gt; {
36 okHttpClientBuilder.connectTimeout(connectTimeout, TimeUnit.SECONDS); 38 okHttpClientBuilder.connectTimeout(connectTimeout, TimeUnit.SECONDS);
37 okHttpClientBuilder.readTimeout(readTimeout, TimeUnit.SECONDS); 39 okHttpClientBuilder.readTimeout(readTimeout, TimeUnit.SECONDS);
38 okHttpClientBuilder.writeTimeout(writeTimeout, TimeUnit.SECONDS); 40 okHttpClientBuilder.writeTimeout(writeTimeout, TimeUnit.SECONDS);
  41 + setInterceptor(okHttpClientBuilder);//设置拦截器
  42 + return new Retrofit.Builder()
  43 + .baseUrl(baseUrl)
  44 + .client(okHttpClientBuilder.build())
  45 + .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
  46 +// .addConverterFactory(GsonConverterFactory.create(true)) //添加Gson转换器
  47 + .addConverterFactory(GsonConverterFactory.create())
  48 + .build()
  49 + .create(netServiceClass);
  50 + }
  51 +
  52 + private void setInterceptor(OkHttpClient.Builder okHttpClientBuilder) {
39 if (null != interceptorList && interceptorList.size() > 0) { 53 if (null != interceptorList && interceptorList.size() > 0) {
40 for (Interceptor interceptor : interceptorList) { 54 for (Interceptor interceptor : interceptorList) {
41 if (null != interceptor) { 55 if (null != interceptor) {
@@ -57,13 +71,6 @@ public class NetBuilder&lt;NetService&gt; { @@ -57,13 +71,6 @@ public class NetBuilder&lt;NetService&gt; {
57 okHttpClientBuilder.addInterceptor(new SignEncryptInterceptor()); 71 okHttpClientBuilder.addInterceptor(new SignEncryptInterceptor());
58 } 72 }
59 } 73 }
60 - return new Retrofit.Builder()  
61 - .baseUrl(baseUrl)  
62 - .client(okHttpClientBuilder.build())  
63 - .addCallAdapterFactory(RxJava2CallAdapterFactory.create())  
64 - .addConverterFactory(GsonConverterFactory.create(true)) //添加Gson转换器  
65 - .build()  
66 - .create(netServiceClass);  
67 } 74 }
68 75
69 /** 76 /**