> entries = params.entrySet();
157 | int count = 0;
158 | for (Map.Entry entry : entries) {
159 | count++;
160 | sb.append(entry.getKey()).append('=').append(Uri.encode(entry.getValue().toString()));
161 | if (count == params.size()) {
162 | break;
163 | }
164 | sb.append('&');
165 | }
166 | url = new String(sb);
167 | }
168 | return url;
169 | }
170 |
171 | /**
172 | * set timeout
173 | */
174 | public void setConnectTimeout(long time) {
175 | if (mOkHttpClient != null) {
176 | mOkHttpClient.setConnectTimeout(time, TimeUnit.MILLISECONDS);
177 | }
178 | }
179 | }
180 |
--------------------------------------------------------------------------------
/app/src/main/java/com/che58/ljb/rxjava/net/interceptor/XgoLogInterceptor.java:
--------------------------------------------------------------------------------
1 | package com.che58.ljb.rxjava.net.interceptor;
2 |
3 | import com.squareup.okhttp.Connection;
4 | import com.squareup.okhttp.Headers;
5 | import com.squareup.okhttp.Interceptor;
6 | import com.squareup.okhttp.MediaType;
7 | import com.squareup.okhttp.Protocol;
8 | import com.squareup.okhttp.Request;
9 | import com.squareup.okhttp.RequestBody;
10 | import com.squareup.okhttp.Response;
11 | import com.squareup.okhttp.ResponseBody;
12 | import com.squareup.okhttp.internal.Platform;
13 | import com.squareup.okhttp.internal.http.HttpEngine;
14 |
15 | import java.io.IOException;
16 | import java.nio.charset.Charset;
17 | import java.util.concurrent.TimeUnit;
18 |
19 | import okio.Buffer;
20 | import okio.BufferedSource;
21 |
22 | /**
23 | * The Http XgoLog filter
24 | * Created by Ljb on 2015/12/11.
25 | */
26 | public class XgoLogInterceptor implements Interceptor {
27 |
28 | private static final String BYTE_BODY = "-byte body)";
29 |
30 | private static final String END = "--> END ";
31 |
32 | private static final Charset UTF8 = Charset.forName("UTF-8");
33 |
34 | private final Logger logger;
35 |
36 | private volatile Level level = Level.NONE;
37 |
38 | public enum Level {
39 | /** No logs. */
40 | NONE,
41 | /**
42 | * Logs request and response lines.
43 | *
44 | * Example:
45 | *
{@code
46 | * --> POST /greeting HTTP/1.1 (3-byte body)
47 | *
48 | * <-- HTTP/1.1 200 OK (22ms, 6-byte body)
49 | * }
50 | */
51 | BASIC,
52 | /**
53 | * Logs request and response lines and their respective headers.
54 | *
55 | * Example:
56 | *
{@code
57 | * --> POST /greeting HTTP/1.1
58 | * Host: example.com
59 | * Content-Type: plain/text
60 | * Content-Length: 3
61 | * --> END POST
62 | *
63 | * <-- HTTP/1.1 200 OK (22ms)
64 | * Content-Type: plain/text
65 | * Content-Length: 6
66 | * <-- END HTTP
67 | * }
68 | */
69 | HEADERS,
70 | /**
71 | * Logs request and response lines and their respective headers and bodies (if present).
72 | *
73 | * Example:
74 | *
{@code
75 | * --> POST /greeting HTTP/1.1
76 | * Host: example.com
77 | * Content-Type: plain/text
78 | * Content-Length: 3
79 | *
80 | * Hi?
81 | * --> END GET
82 | *
83 | * <-- HTTP/1.1 200 OK (22ms)
84 | * Content-Type: plain/text
85 | * Content-Length: 6
86 | *
87 | * Hello!
88 | * <-- END HTTP
89 | * }
90 | */
91 | BODY
92 | }
93 |
94 | public interface Logger {
95 | /** A {@link Logger} defaults output appropriate for the current platform. */
96 | Logger DEFAULT = new Logger() {
97 | @Override
98 | public void log(String message) {
99 | Platform.get().log(message);
100 | }
101 | };
102 |
103 | void log(String message);
104 |
105 | }
106 |
107 | public XgoLogInterceptor() {
108 | this(Logger.DEFAULT);
109 | }
110 |
111 | public XgoLogInterceptor(Logger logger) {
112 | this.logger = logger;
113 | }
114 |
115 | /** Change the level at which this interceptor logs. */
116 | public XgoLogInterceptor setLevel(Level level) {
117 | if (level == null) throw new NullPointerException("level == null. Use Level.NONE instead.");
118 | this.level = level;
119 | return this;
120 | }
121 |
122 | public Level getLevel() {
123 | return level;
124 | }
125 |
126 | @Override
127 | public Response intercept(Chain chain) throws IOException {
128 | Level level = this.level;
129 |
130 | Request request = chain.request();
131 | if (level == Level.NONE) {
132 | return chain.proceed(request);
133 | }
134 |
135 | boolean logBody = level == Level.BODY;
136 | boolean logHeaders = logBody || level == Level.HEADERS;
137 |
138 | RequestBody requestBody = request.body();
139 | boolean hasRequestBody = requestBody != null;
140 |
141 | Connection connection = chain.connection();
142 | Protocol protocol = connection != null ? connection.getProtocol() : Protocol.HTTP_1_1;
143 | StringBuilder requestStartMessage = new StringBuilder();
144 | requestStartMessage.append("--> " + request.method() + ' ' + request.url() + ' ' + protocol(protocol));
145 | if (!logHeaders && hasRequestBody) {
146 | requestStartMessage.append(" (" + requestBody.contentLength() + BYTE_BODY);
147 | }
148 | logger.log(requestStartMessage.toString());
149 |
150 | if (logHeaders) {
151 | if (hasRequestBody) {
152 | // Request body headers are only present when installed as a network interceptor. Force
153 | // them to be included (when available) so there values are known.
154 | if (requestBody.contentType() != null) {
155 | logger.log("Content-Type: " + requestBody.contentType());
156 | }
157 | if (requestBody.contentLength() != -1) {
158 | logger.log("Content-Length: " + requestBody.contentLength());
159 | }
160 | }
161 |
162 | Headers headers = request.headers();
163 | for (int i = 0, count = headers.size(); i < count; i++) {
164 | String name = headers.name(i);
165 | // Skip headers from the request body as they are explicitly logged above.
166 | if (!"Content-Type".equalsIgnoreCase(name) && !"Content-Length".equalsIgnoreCase(name)) {
167 | logger.log(name + ": " + headers.value(i));
168 | }
169 | }
170 |
171 | if (!logBody || !hasRequestBody) {
172 | logger.log(END + request.method());
173 | } else if (bodyEncoded(request.headers())) {
174 | logger.log(END + request.method() + " (encoded body omitted)");
175 | } else {
176 | Buffer buffer = new Buffer();
177 | requestBody.writeTo(buffer);
178 |
179 | Charset charset = UTF8;
180 | MediaType contentType = requestBody.contentType();
181 | if (contentType != null) {
182 | contentType.charset(UTF8);
183 | }
184 |
185 | logger.log("");
186 | logger.log(buffer.readString(charset));
187 |
188 | logger.log(END + request.method()
189 | + " (" + requestBody.contentLength() + BYTE_BODY);
190 | }
191 | }
192 |
193 | long startNs = System.nanoTime();
194 | Response response = chain.proceed(request);
195 | long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
196 |
197 | ResponseBody responseBody = response.body();
198 | logger.log("<-- " + response.code() + ' ' + response.message() + ' '
199 | + response.request().url() + " (" + tookMs + "ms" + (!logHeaders ? ", "
200 | + responseBody.contentLength() + "-byte body" : "") + ')');
201 |
202 | if (logHeaders) {
203 | Headers headers = response.headers();
204 | for (int i = 0, count = headers.size(); i < count; i++) {
205 | logger.log(headers.name(i) + ": " + headers.value(i));
206 | }
207 |
208 | if (!logBody || !HttpEngine.hasBody(response)) {
209 | logger.log("<-- END HTTP");
210 | } else if (bodyEncoded(response.headers())) {
211 | logger.log("<-- END HTTP (encoded body omitted)");
212 | } else {
213 | BufferedSource source = responseBody.source();
214 | source.request(Long.MAX_VALUE); // Buffer the entire body.
215 | Buffer buffer = source.buffer();
216 |
217 | Charset charset = UTF8;
218 | MediaType contentType = responseBody.contentType();
219 | if (contentType != null) {
220 | charset = contentType.charset(UTF8);
221 | }
222 |
223 | if (responseBody.contentLength() != 0) {
224 | logger.log("");
225 | logger.log(buffer.clone().readString(charset));
226 | }
227 |
228 | logger.log("<-- END HTTP (" + buffer.size() + BYTE_BODY);
229 | }
230 | }
231 |
232 | return response;
233 | }
234 |
235 | private boolean bodyEncoded(Headers headers) {
236 | String contentEncoding = headers.get("Content-Encoding");
237 | return contentEncoding != null && !contentEncoding.equalsIgnoreCase("identity");
238 | }
239 |
240 | private static String protocol(Protocol protocol) {
241 | return protocol == Protocol.HTTP_1_0 ? "HTTP/1.0" : "HTTP/1.1";
242 | }
243 |
244 | }
245 |
--------------------------------------------------------------------------------
/app/src/main/java/com/che58/ljb/rxjava/protocol/BaseProtocol.java:
--------------------------------------------------------------------------------
1 | package com.che58.ljb.rxjava.protocol;
2 |
3 | import android.text.TextUtils;
4 |
5 | import com.che58.ljb.rxjava.net.XgoHttpClient;
6 | import com.squareup.okhttp.Request;
7 |
8 | import java.util.Map;
9 |
10 | import rx.Observable;
11 | import rx.Subscriber;
12 | import rx.schedulers.Schedulers;
13 |
14 | /**
15 | * BaseProtocol
16 | * Created by Ljb on 2015/12/22.
17 | */
18 | public abstract class BaseProtocol {
19 |
20 |
21 | /**
22 | * 创建一个工作在IO线程的被观察者(被订阅者)对象
23 | * @param url
24 | * @param method
25 | * @param params
26 | */
27 | protected Observable createObservable(final String url, final String method, final Map params) {
28 | return Observable.create(new Observable.OnSubscribe() { // (2)
29 | @Override
30 | public void call(Subscriber super String> subscriber) {
31 | Request request = XgoHttpClient.getClient().getRequest(url, method, params); // (3)
32 | String json = XgoHttpClient.getClient().execute2String(request); // (4)
33 | setData(subscriber, json); // (5)
34 | }
35 | }).subscribeOn(Schedulers.io());
36 | }
37 |
38 |
39 | /**
40 | * 为观察者(订阅者)设置返回数据
41 | * */
42 | protected void setData(Subscriber super String> subscriber, String json) {
43 | if (TextUtils.isEmpty(json)) { // (6)
44 | subscriber.onError(new Throwable("not data"));
45 | return;
46 | }
47 | subscriber.onNext(json); // (7)
48 | subscriber.onCompleted();
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/app/src/main/java/com/che58/ljb/rxjava/protocol/TestProtocol.java:
--------------------------------------------------------------------------------
1 | package com.che58.ljb.rxjava.protocol;
2 |
3 | import com.che58.ljb.rxjava.net.XgoHttpClient;
4 |
5 | import java.util.Map;
6 |
7 | import rx.Observable;
8 |
9 | /**
10 | * 测试接口
11 | * Created by ljb on 2016/3/23.
12 | */
13 | public class TestProtocol extends BaseProtocol {
14 |
15 | private static final String URL = "http://integer.wang/init/json.shtml";
16 |
17 | /**
18 | * Get请求
19 | */
20 | public Observable testGet() {
21 | return createObservable(URL, XgoHttpClient.METHOD_GET, null); // (1)
22 | }
23 |
24 |
25 | /**
26 | * Post请求
27 | */
28 | public Observable testPost(Map params) {
29 | return createObservable(URL, XgoHttpClient.METHOD_POST, params);
30 | }
31 |
32 | /**
33 | * Put请求
34 | */
35 | public Observable testPut(Map params) {
36 | return createObservable(URL, XgoHttpClient.METHOD_PUT, params);
37 | }
38 |
39 | /**
40 | * Delete请求
41 | */
42 | public Observable testDelete() {
43 | return createObservable(URL, XgoHttpClient.METHOD_DELETE, null);
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/app/src/main/java/com/che58/ljb/rxjava/protocol2/BaseProtocol.java:
--------------------------------------------------------------------------------
1 | package com.che58.ljb.rxjava.protocol2;
2 |
3 | import android.text.TextUtils;
4 |
5 | import com.che58.ljb.rxjava.net.XgoHttpClient;
6 | import com.google.gson.Gson;
7 | import com.squareup.okhttp.Request;
8 |
9 | import java.util.Map;
10 |
11 | import rx.Observable;
12 | import rx.Subscriber;
13 | import rx.schedulers.Schedulers;
14 |
15 | /**
16 | * BaseProtocol 加入Gson解析
17 | * Created by Ljb on 2015/12/22.
18 | */
19 | public abstract class BaseProtocol {
20 |
21 | private static final Gson mGson;
22 |
23 | static {
24 | mGson = new Gson();
25 | }
26 |
27 |
28 | /**
29 | * 创建一个工作在IO线程的被观察者(被订阅者)对象
30 | *
31 | * @param url
32 | * @param method
33 | * @param params
34 | */
35 | protected Observable createObservable(final String url, final String method, final Map params, final Class clazz) {
36 | return Observable.create(new Observable.OnSubscribe() {
37 | @Override
38 | public void call(Subscriber super T> subscriber) {
39 | Request request = XgoHttpClient.getClient().getRequest(url, method, params);
40 | String json = XgoHttpClient.getClient().execute2String(request);
41 | setData(subscriber, json, clazz);
42 | }
43 | }).subscribeOn(Schedulers.io());
44 | }
45 |
46 |
47 | /**
48 | * 为观察者(订阅者)设置返回数据
49 | */
50 | protected void setData(Subscriber super T> subscriber, String json, Class clazz) {
51 | if (TextUtils.isEmpty(json)) {
52 | subscriber.onError(new Throwable("not data"));
53 | return;
54 | }
55 |
56 | T data = mGson.fromJson(json, clazz);
57 |
58 | subscriber.onNext(data);
59 | subscriber.onCompleted();
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/app/src/main/java/com/che58/ljb/rxjava/protocol2/TestProtocol.java:
--------------------------------------------------------------------------------
1 | package com.che58.ljb.rxjava.protocol2;
2 |
3 | import com.che58.ljb.rxjava.model.DeleteModel;
4 | import com.che58.ljb.rxjava.model.GetModel;
5 | import com.che58.ljb.rxjava.model.PostModel;
6 | import com.che58.ljb.rxjava.model.PutModel;
7 | import com.che58.ljb.rxjava.net.XgoHttpClient;
8 |
9 | import java.util.Map;
10 |
11 | import rx.Observable;
12 |
13 | /**
14 | * 测试接口
15 | * Created by ljb on 2016/3/23.
16 | */
17 | public class TestProtocol extends BaseProtocol {
18 |
19 | private static final String URL = "http://integer.wang/init/json.shtml";
20 |
21 | /**
22 | * Get请求
23 | */
24 | public Observable testGet() {
25 | return createObservable(URL, XgoHttpClient.METHOD_GET, null, GetModel.class);
26 | }
27 |
28 |
29 | /**
30 | * Post请求
31 | */
32 | public Observable testPost(Map params) {
33 | return createObservable(URL, XgoHttpClient.METHOD_POST, params, PostModel.class);
34 | }
35 |
36 | /**
37 | * Put请求
38 | */
39 | public Observable testPut(Map params) {
40 | return createObservable(URL, XgoHttpClient.METHOD_PUT, params, PutModel.class);
41 | }
42 |
43 | /**
44 | * Delete请求
45 | */
46 | public Observable testDelete() {
47 | return createObservable(URL, XgoHttpClient.METHOD_DELETE, null, DeleteModel.class);
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/app/src/main/java/com/che58/ljb/rxjava/rxbus/RxBus.java:
--------------------------------------------------------------------------------
1 | package com.che58.ljb.rxjava.rxbus;
2 |
3 | import rx.Observable;
4 | import rx.subjects.PublishSubject;
5 | import rx.subjects.SerializedSubject;
6 | import rx.subjects.Subject;
7 |
8 | /**
9 | * RxBus
10 | */
11 | public class RxBus {
12 |
13 | //private final PublishSubject