> getAllRequests() {
124 | return mRequestQueue;
125 | }
126 |
127 | /**
128 | * 为每个请求生成一个系列号
129 | *
130 | * @return 序列号
131 | */
132 | private int generateSerialNumber() {
133 | return mSerialNumGenerator.incrementAndGet();
134 | }
135 | }
136 |
--------------------------------------------------------------------------------
/network/SimpleNet/src/org/simple/net/core/ResponseDelivery.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 bboyfeiyu@gmail.com, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.net.core;
26 |
27 | import android.os.Handler;
28 | import android.os.Looper;
29 |
30 | import org.simple.net.base.Request;
31 | import org.simple.net.base.Response;
32 |
33 | import java.util.concurrent.Executor;
34 |
35 | /**
36 | * 请求结果投递类,将请求结果投递给UI线程
37 | *
38 | * @author mrsimple
39 | */
40 | class ResponseDelivery implements Executor {
41 |
42 | /**
43 | * 主线程的hander
44 | */
45 | Handler mResponseHandler = new Handler(Looper.getMainLooper());
46 |
47 | /**
48 | * 处理请求结果,将其执行在UI线程
49 | *
50 | * @param request
51 | * @param response
52 | */
53 | public void deliveryResponse(final Request> request, final Response response) {
54 | Runnable respRunnable = new Runnable() {
55 |
56 | @Override
57 | public void run() {
58 | request.deliveryResponse(response);
59 | }
60 | };
61 |
62 | execute(respRunnable);
63 | }
64 |
65 | @Override
66 | public void execute(Runnable command) {
67 | mResponseHandler.post(command);
68 | }
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/network/SimpleNet/src/org/simple/net/core/SimpleNet.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 bboyfeiyu@gmail.com
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.net.core;
26 |
27 | import org.simple.net.httpstacks.HttpStack;
28 |
29 | /**
30 | * SimpleNet入口
31 | * @author mrsimple
32 | */
33 | public final class SimpleNet {
34 | /**
35 | * 创建一个请求队列,NetworkExecutor数量为默认的数量
36 | *
37 | * @return
38 | */
39 | public static RequestQueue newRequestQueue() {
40 | return newRequestQueue(RequestQueue.DEFAULT_CORE_NUMS);
41 | }
42 |
43 | /**
44 | * 创建一个请求队列,NetworkExecutor数量为coreNums
45 | *
46 | * @param coreNums
47 | * @return
48 | */
49 | public static RequestQueue newRequestQueue(int coreNums) {
50 | return newRequestQueue(coreNums, null);
51 | }
52 |
53 | /**
54 | * 创建一个请求队列,NetworkExecutor数量为coreNums
55 | *
56 | * @param coreNums 线程数量
57 | * @param httpStack 网络执行者
58 | * @return
59 | */
60 | public static RequestQueue newRequestQueue(int coreNums, HttpStack httpStack) {
61 | RequestQueue queue = new RequestQueue(Math.max(0, coreNums), httpStack);
62 | queue.start();
63 | return queue;
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/network/SimpleNet/src/org/simple/net/entity/MultipartEntity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 bboyfeiyu@gmail.com, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.net.entity;
26 |
27 | import android.text.TextUtils;
28 |
29 | import org.apache.http.Header;
30 | import org.apache.http.HttpEntity;
31 | import org.apache.http.message.BasicHeader;
32 |
33 | import java.io.ByteArrayInputStream;
34 | import java.io.ByteArrayOutputStream;
35 | import java.io.Closeable;
36 | import java.io.File;
37 | import java.io.FileInputStream;
38 | import java.io.IOException;
39 | import java.io.InputStream;
40 | import java.io.OutputStream;
41 | import java.util.Random;
42 |
43 | /**
44 | * POST报文格式请参考博客 : http://blog.csdn.net/bboyfeiyu/article/details/41863951.
45 | *
46 | * Android中的多参数类型的Entity实体类,用户可以使用该类来上传文件、文本参数、二进制参数,
47 | * 不需要依赖于httpmime.jar来实现上传文件的功能.
48 | *
49 | *
50 | * @author mrsimple
51 | */
52 | public class MultipartEntity implements HttpEntity {
53 |
54 | private final static char[] MULTIPART_CHARS = "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
55 | .toCharArray();
56 | /**
57 | * 换行符
58 | */
59 | private final String NEW_LINE_STR = "\r\n";
60 | private final String CONTENT_TYPE = "Content-Type: ";
61 | private final String CONTENT_DISPOSITION = "Content-Disposition: ";
62 | /**
63 | * 文本参数和字符集
64 | */
65 | private final String TYPE_TEXT_CHARSET = "text/plain; charset=UTF-8";
66 |
67 | /**
68 | * 字节流参数
69 | */
70 | private final String TYPE_OCTET_STREAM = "application/octet-stream";
71 | /**
72 | * 二进制参数
73 | */
74 | private final byte[] BINARY_ENCODING = "Content-Transfer-Encoding: binary\r\n\r\n".getBytes();
75 | /**
76 | * 文本参数
77 | */
78 | private final byte[] BIT_ENCODING = "Content-Transfer-Encoding: 8bit\r\n\r\n".getBytes();
79 |
80 | /**
81 | * 分隔符
82 | */
83 | private String mBoundary = null;
84 | /**
85 | * 输出流
86 | */
87 | ByteArrayOutputStream mOutputStream = new ByteArrayOutputStream();
88 |
89 | public MultipartEntity() {
90 | this.mBoundary = generateBoundary();
91 | }
92 |
93 | /**
94 | * 生成分隔符
95 | *
96 | * @return
97 | */
98 | private final String generateBoundary() {
99 | final StringBuffer buf = new StringBuffer();
100 | final Random rand = new Random();
101 | for (int i = 0; i < 30; i++) {
102 | buf.append(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]);
103 | }
104 | return buf.toString();
105 | }
106 |
107 | /**
108 | * 参数开头的分隔符
109 | *
110 | * @throws IOException
111 | */
112 | private void writeFirstBoundary() throws IOException {
113 | mOutputStream.write(("--" + mBoundary + "\r\n").getBytes());
114 | }
115 |
116 | /**
117 | * 添加文本参数
118 | *
119 | * @param key
120 | * @param value
121 | */
122 | public void addStringPart(final String paramName, final String value) {
123 | writeToOutputStream(paramName, value.getBytes(), TYPE_TEXT_CHARSET, BIT_ENCODING, "");
124 | }
125 |
126 | /**
127 | * 将数据写入到输出流中
128 | *
129 | * @param key
130 | * @param rawData
131 | * @param type
132 | * @param encodingBytes
133 | * @param fileName
134 | */
135 | private void writeToOutputStream(String paramName, byte[] rawData, String type,
136 | byte[] encodingBytes,
137 | String fileName) {
138 | try {
139 | writeFirstBoundary();
140 | mOutputStream.write((CONTENT_TYPE + type + NEW_LINE_STR).getBytes());
141 | mOutputStream
142 | .write(getContentDispositionBytes(paramName, fileName));
143 | mOutputStream.write(encodingBytes);
144 | mOutputStream.write(rawData);
145 | mOutputStream.write(NEW_LINE_STR.getBytes());
146 | } catch (final IOException e) {
147 | e.printStackTrace();
148 | }
149 | }
150 |
151 | /**
152 | * 添加二进制参数, 例如Bitmap的字节流参数
153 | *
154 | * @param key
155 | * @param rawData
156 | */
157 | public void addBinaryPart(String paramName, final byte[] rawData) {
158 | writeToOutputStream(paramName, rawData, TYPE_OCTET_STREAM, BINARY_ENCODING, "no-file");
159 | }
160 |
161 | /**
162 | * 添加文件参数,可以实现文件上传功能
163 | *
164 | * @param key
165 | * @param file
166 | */
167 | public void addFilePart(final String key, final File file) {
168 | InputStream fin = null;
169 | try {
170 | fin = new FileInputStream(file);
171 | writeFirstBoundary();
172 | final String type = CONTENT_TYPE + TYPE_OCTET_STREAM + NEW_LINE_STR;
173 | mOutputStream.write(getContentDispositionBytes(key, file.getName()));
174 | mOutputStream.write(type.getBytes());
175 | mOutputStream.write(BINARY_ENCODING);
176 |
177 | final byte[] tmp = new byte[4096];
178 | int len = 0;
179 | while ((len = fin.read(tmp)) != -1) {
180 | mOutputStream.write(tmp, 0, len);
181 | }
182 | mOutputStream.flush();
183 | } catch (final IOException e) {
184 | e.printStackTrace();
185 | } finally {
186 | closeSilently(fin);
187 | }
188 | }
189 |
190 | private void closeSilently(Closeable closeable) {
191 | try {
192 | if (closeable != null) {
193 | closeable.close();
194 | }
195 | } catch (final IOException e) {
196 | e.printStackTrace();
197 | }
198 | }
199 |
200 | private byte[] getContentDispositionBytes(String paramName, String fileName) {
201 | StringBuilder stringBuilder = new StringBuilder();
202 | stringBuilder.append(CONTENT_DISPOSITION + "form-data; name=\"" + paramName + "\"");
203 | // 文本参数没有filename参数,设置为空即可
204 | if (!TextUtils.isEmpty(fileName)) {
205 | stringBuilder.append("; filename=\""
206 | + fileName + "\"");
207 | }
208 |
209 | return stringBuilder.append(NEW_LINE_STR).toString().getBytes();
210 | }
211 |
212 | @Override
213 | public long getContentLength() {
214 | return mOutputStream.toByteArray().length;
215 | }
216 |
217 | @Override
218 | public Header getContentType() {
219 | return new BasicHeader("Content-Type", "multipart/form-data; boundary=" + mBoundary);
220 | }
221 |
222 | @Override
223 | public boolean isChunked() {
224 | return false;
225 | }
226 |
227 | @Override
228 | public boolean isRepeatable() {
229 | return false;
230 | }
231 |
232 | @Override
233 | public boolean isStreaming() {
234 | return false;
235 | }
236 |
237 | @Override
238 | public void writeTo(final OutputStream outstream) throws IOException {
239 | // 参数最末尾的结束符
240 | final String endString = "--" + mBoundary + "--\r\n";
241 | // 写入结束符
242 | mOutputStream.write(endString.getBytes());
243 | //
244 | outstream.write(mOutputStream.toByteArray());
245 | }
246 |
247 | @Override
248 | public Header getContentEncoding() {
249 | return null;
250 | }
251 |
252 | @Override
253 | public void consumeContent() throws IOException,
254 | UnsupportedOperationException {
255 | if (isStreaming()) {
256 | throw new UnsupportedOperationException(
257 | "Streaming entity does not implement #consumeContent()");
258 | }
259 | }
260 |
261 | @Override
262 | public InputStream getContent() {
263 | return new ByteArrayInputStream(mOutputStream.toByteArray());
264 | }
265 | }
266 |
--------------------------------------------------------------------------------
/network/SimpleNet/src/org/simple/net/httpstacks/HttpClientStack.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 bboyfeiyu@gmail.com, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.net.httpstacks;
26 |
27 | import android.net.http.AndroidHttpClient;
28 |
29 | import org.apache.http.HttpEntity;
30 | import org.apache.http.HttpResponse;
31 | import org.apache.http.client.HttpClient;
32 | import org.apache.http.client.methods.HttpDelete;
33 | import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
34 | import org.apache.http.client.methods.HttpGet;
35 | import org.apache.http.client.methods.HttpPost;
36 | import org.apache.http.client.methods.HttpPut;
37 | import org.apache.http.client.methods.HttpUriRequest;
38 | import org.apache.http.conn.scheme.Scheme;
39 | import org.apache.http.conn.ssl.SSLSocketFactory;
40 | import org.apache.http.entity.ByteArrayEntity;
41 | import org.apache.http.params.HttpConnectionParams;
42 | import org.apache.http.params.HttpParams;
43 | import org.simple.net.base.Request;
44 | import org.simple.net.base.Response;
45 | import org.simple.net.config.HttpClientConfig;
46 |
47 | import java.util.Map;
48 |
49 | /**
50 | * api 9以下使用HttpClient执行网络请求, https配置参考http://jackyrong.iteye.com/blog/1606444
51 | *
52 | * @author mrsimple
53 | */
54 | public class HttpClientStack implements HttpStack {
55 |
56 | /**
57 | * 使用HttpClient执行网络请求时的Https配置
58 | */
59 | HttpClientConfig mConfig = HttpClientConfig.getConfig();
60 | /**
61 | * HttpClient
62 | */
63 | HttpClient mHttpClient = AndroidHttpClient.newInstance(mConfig.userAgent);
64 |
65 | @Override
66 | public Response performRequest(Request> request) {
67 | try {
68 | HttpUriRequest httpRequest = createHttpRequest(request);
69 | // 添加连接参数
70 | setConnectionParams(httpRequest);
71 | // 添加header
72 | addHeaders(httpRequest, request.getHeaders());
73 | // https配置
74 | configHttps(request);
75 | // 执行请求
76 | HttpResponse response = mHttpClient.execute(httpRequest);
77 | // 构建Response
78 | Response rawResponse = new Response(response.getStatusLine());
79 | // 设置Entity
80 | rawResponse.setEntity(response.getEntity());
81 | return rawResponse;
82 | } catch (Exception e) {
83 | }
84 |
85 | return null;
86 | }
87 |
88 | /**
89 | * 如果是https请求,则使用用户配置的SSLSocketFactory进行配置.
90 | *
91 | * @param request
92 | */
93 | private void configHttps(Request> request) {
94 | SSLSocketFactory sslSocketFactory = mConfig.getSocketFactory();
95 | if (request.isHttps() && sslSocketFactory != null) {
96 | Scheme sch = new Scheme("https", sslSocketFactory, 443);
97 | mHttpClient.getConnectionManager().getSchemeRegistry().register(sch);
98 | }
99 | }
100 |
101 | /**
102 | * 设置连接参数,这里比较简单啊.一些优化设置就没有写了.
103 | *
104 | * @param httpUriRequest
105 | */
106 | private void setConnectionParams(HttpUriRequest httpUriRequest) {
107 | HttpParams httpParams = httpUriRequest.getParams();
108 | HttpConnectionParams.setConnectionTimeout(httpParams, mConfig.connTimeOut);
109 | HttpConnectionParams.setSoTimeout(httpParams, mConfig.soTimeOut);
110 | }
111 |
112 | /**
113 | * 根据请求类型创建不同的Http请求
114 | *
115 | * @param request
116 | * @return
117 | */
118 | static HttpUriRequest createHttpRequest(Request> request) {
119 | HttpUriRequest httpUriRequest = null;
120 | switch (request.getHttpMethod()) {
121 | case GET:
122 | httpUriRequest = new HttpGet(request.getUrl());
123 | break;
124 | case DELETE:
125 | httpUriRequest = new HttpDelete(request.getUrl());
126 | break;
127 | case POST: {
128 | httpUriRequest = new HttpPost(request.getUrl());
129 | httpUriRequest.addHeader(Request.HEADER_CONTENT_TYPE, request.getBodyContentType());
130 | setEntityIfNonEmptyBody((HttpPost) httpUriRequest, request);
131 | }
132 | break;
133 | case PUT: {
134 | httpUriRequest = new HttpPut(request.getUrl());
135 | httpUriRequest.addHeader(Request.HEADER_CONTENT_TYPE, request.getBodyContentType());
136 | setEntityIfNonEmptyBody((HttpPut) httpUriRequest, request);
137 | }
138 | break;
139 | default:
140 | throw new IllegalStateException("Unknown request method.");
141 | }
142 |
143 | return httpUriRequest;
144 | }
145 |
146 | private static void addHeaders(HttpUriRequest httpRequest, Map headers) {
147 | for (String key : headers.keySet()) {
148 | httpRequest.setHeader(key, headers.get(key));
149 | }
150 | }
151 |
152 | /**
153 | * 将请求参数设置到HttpEntity中
154 | *
155 | * @param httpRequest
156 | * @param request
157 | */
158 | private static void setEntityIfNonEmptyBody(HttpEntityEnclosingRequestBase httpRequest,
159 | Request> request) {
160 | byte[] body = request.getBody();
161 | if (body != null) {
162 | HttpEntity entity = new ByteArrayEntity(body);
163 | httpRequest.setEntity(entity);
164 | }
165 | }
166 | }
167 |
--------------------------------------------------------------------------------
/network/SimpleNet/src/org/simple/net/httpstacks/HttpStack.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 bboyfeiyu@gmail.com, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.net.httpstacks;
26 |
27 | import org.simple.net.base.Request;
28 | import org.simple.net.base.Response;
29 |
30 | /**
31 | * 执行网络请求的接口
32 | *
33 | * @author mrsimple
34 | */
35 | public interface HttpStack {
36 | /**
37 | * 执行Http请求
38 | *
39 | * @param request 待执行的请求
40 | * @return
41 | */
42 | public Response performRequest(Request> request);
43 | }
44 |
--------------------------------------------------------------------------------
/network/SimpleNet/src/org/simple/net/httpstacks/HttpStackFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 bboyfeiyu@gmail.com, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.net.httpstacks;
26 |
27 | import android.os.Build;
28 |
29 | /**
30 | * 根据api版本选择HttpClient或者HttpURLConnection
31 | *
32 | * @author mrsimple
33 | */
34 | public final class HttpStackFactory {
35 |
36 | private static final int GINGERBREAD_SDK_NUM = 9;
37 |
38 | /**
39 | * 根据SDK版本号来创建不同的Http执行器,即SDK 9之前使用HttpClient,之后则使用HttlUrlConnection,
40 | * 两者之间的差别请参考 :
41 | * http://android-developers.blogspot.com/2011/09/androids-http-clients.html
42 | *
43 | * @return
44 | */
45 | public static HttpStack createHttpStack() {
46 | int runtimeSDKApi = Build.VERSION.SDK_INT;
47 | if (runtimeSDKApi >= GINGERBREAD_SDK_NUM) {
48 | return new HttpUrlConnStack();
49 | }
50 |
51 | return new HttpClientStack();
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/network/SimpleNet/src/org/simple/net/httpstacks/HttpUrlConnStack.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 bboyfeiyu@gmail.com, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.net.httpstacks;
26 |
27 | import org.apache.http.Header;
28 | import org.apache.http.HttpEntity;
29 | import org.apache.http.ProtocolVersion;
30 | import org.apache.http.StatusLine;
31 | import org.apache.http.entity.BasicHttpEntity;
32 | import org.apache.http.message.BasicHeader;
33 | import org.apache.http.message.BasicHttpResponse;
34 | import org.apache.http.message.BasicStatusLine;
35 | import org.simple.net.base.Request;
36 | import org.simple.net.base.Request.HttpMethod;
37 | import org.simple.net.base.Response;
38 | import org.simple.net.config.HttpUrlConnConfig;
39 |
40 | import java.io.DataOutputStream;
41 | import java.io.IOException;
42 | import java.io.InputStream;
43 | import java.net.HttpURLConnection;
44 | import java.net.ProtocolException;
45 | import java.net.URL;
46 | import java.net.URLConnection;
47 | import java.util.List;
48 | import java.util.Map.Entry;
49 | import java.util.Set;
50 |
51 | import javax.net.ssl.HttpsURLConnection;
52 | import javax.net.ssl.SSLSocketFactory;
53 |
54 | /**
55 | * 使用HttpURLConnection执行网络请求的HttpStack
56 | *
57 | * @author mrsimple
58 | */
59 | public class HttpUrlConnStack implements HttpStack {
60 |
61 | /**
62 | * 配置Https
63 | */
64 | HttpUrlConnConfig mConfig = HttpUrlConnConfig.getConfig();
65 |
66 | @Override
67 | public Response performRequest(Request> request) {
68 | HttpURLConnection urlConnection = null;
69 | try {
70 | // 构建HttpURLConnection
71 | urlConnection = createUrlConnection(request.getUrl());
72 | // 设置headers
73 | setRequestHeaders(urlConnection, request);
74 | // 设置Body参数
75 | setRequestParams(urlConnection, request);
76 | // https 配置
77 | configHttps(request);
78 | return fetchResponse(urlConnection);
79 | } catch (Exception e) {
80 | e.printStackTrace();
81 | } finally {
82 | if (urlConnection != null) {
83 | urlConnection.disconnect();
84 | }
85 | }
86 | return null;
87 | }
88 |
89 | private HttpURLConnection createUrlConnection(String url) throws IOException {
90 | URL newURL = new URL(url);
91 | URLConnection urlConnection = newURL.openConnection();
92 | urlConnection.setConnectTimeout(mConfig.connTimeOut);
93 | urlConnection.setReadTimeout(mConfig.soTimeOut);
94 | urlConnection.setDoInput(true);
95 | urlConnection.setUseCaches(false);
96 | return (HttpURLConnection) urlConnection;
97 | }
98 |
99 | private void configHttps(Request> request) {
100 | if (request.isHttps()) {
101 | SSLSocketFactory sslFactory = mConfig.getSslSocketFactory();
102 | // 配置https
103 | if (sslFactory != null) {
104 | HttpsURLConnection.setDefaultSSLSocketFactory(sslFactory);
105 | HttpsURLConnection.setDefaultHostnameVerifier(mConfig.getHostnameVerifier());
106 | }
107 |
108 | }
109 | }
110 |
111 | private void setRequestHeaders(HttpURLConnection connection, Request> request) {
112 | Set headersKeys = request.getHeaders().keySet();
113 | for (String headerName : headersKeys) {
114 | connection.addRequestProperty(headerName, request.getHeaders().get(headerName));
115 | }
116 | }
117 |
118 | protected void setRequestParams(HttpURLConnection connection, Request> request)
119 | throws ProtocolException, IOException {
120 | HttpMethod method = request.getHttpMethod();
121 | connection.setRequestMethod(method.toString());
122 | // add params
123 | byte[] body = request.getBody();
124 | if (body != null) {
125 | // enable output
126 | connection.setDoOutput(true);
127 | // set content type
128 | connection
129 | .addRequestProperty(Request.HEADER_CONTENT_TYPE, request.getBodyContentType());
130 | // write params data to connection
131 | DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
132 | dataOutputStream.write(body);
133 | dataOutputStream.close();
134 | }
135 | }
136 |
137 | private Response fetchResponse(HttpURLConnection connection) throws IOException {
138 |
139 | // Initialize HttpResponse with data from the HttpURLConnection.
140 | ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
141 | int responseCode = connection.getResponseCode();
142 | if (responseCode == -1) {
143 | throw new IOException("Could not retrieve response code from HttpUrlConnection.");
144 | }
145 | // 状态行数据
146 | StatusLine responseStatus = new BasicStatusLine(protocolVersion,
147 | connection.getResponseCode(), connection.getResponseMessage());
148 | // 构建response
149 | Response response = new Response(responseStatus);
150 | // 设置response数据
151 | response.setEntity(entityFromURLConnwction(connection));
152 | addHeadersToResponse(response, connection);
153 | return response;
154 | }
155 |
156 | /**
157 | * 执行HTTP请求之后获取到其数据流,即返回请求结果的流
158 | *
159 | * @param connection
160 | * @return
161 | */
162 | private HttpEntity entityFromURLConnwction(HttpURLConnection connection) {
163 | BasicHttpEntity entity = new BasicHttpEntity();
164 | InputStream inputStream = null;
165 | try {
166 | inputStream = connection.getInputStream();
167 | } catch (IOException e) {
168 | e.printStackTrace();
169 | inputStream = connection.getErrorStream();
170 | }
171 |
172 | // TODO : GZIP
173 | entity.setContent(inputStream);
174 | entity.setContentLength(connection.getContentLength());
175 | entity.setContentEncoding(connection.getContentEncoding());
176 | entity.setContentType(connection.getContentType());
177 |
178 | return entity;
179 | }
180 |
181 | private void addHeadersToResponse(BasicHttpResponse response, HttpURLConnection connection) {
182 | for (Entry> header : connection.getHeaderFields().entrySet()) {
183 | if (header.getKey() != null) {
184 | Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
185 | response.addHeader(h);
186 | }
187 | }
188 | }
189 |
190 | }
191 |
--------------------------------------------------------------------------------
/network/SimpleNet/src/org/simple/net/requests/JsonRequest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 bboyfeiyu@gmail.com, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.net.requests;
26 |
27 | import org.json.JSONException;
28 | import org.json.JSONObject;
29 | import org.simple.net.base.Request;
30 | import org.simple.net.base.Response;
31 |
32 | /**
33 | * 返回的数据类型为Json的请求, Json对应的对象类型为JSONObject
34 | *
35 | * @author mrsimple
36 | */
37 | public class JsonRequest extends Request {
38 |
39 | public JsonRequest(HttpMethod method, String url, RequestListener listener) {
40 | super(method, url, listener);
41 | }
42 |
43 |
44 | /**
45 | * 将Response的结果转换为JSONObject
46 | */
47 | @Override
48 | public JSONObject parseResponse(Response response) {
49 | String jsonString = new String(response.getRawData());
50 | try {
51 | return new JSONObject(jsonString);
52 | } catch (JSONException e) {
53 | e.printStackTrace();
54 | }
55 | return null;
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/network/SimpleNet/src/org/simple/net/requests/MultipartRequest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 bboyfeiyu@gmail.com, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.net.requests;
26 |
27 | import android.util.Log;
28 |
29 | import org.simple.net.base.Request;
30 | import org.simple.net.base.Response;
31 | import org.simple.net.entity.MultipartEntity;
32 |
33 | import java.io.ByteArrayOutputStream;
34 | import java.io.IOException;
35 |
36 | /**
37 | * Multipart请求 ( 只能为POST请求 ),该请求可以搭载多种类型参数,比如文本、文件等,但是文件仅限于小文件,否则会出现OOM异常.
38 | *
39 | * @author mrsimple
40 | */
41 | public class MultipartRequest extends Request {
42 |
43 | MultipartEntity mMultiPartEntity = new MultipartEntity();
44 |
45 | public MultipartRequest(String url, RequestListener listener) {
46 | super(HttpMethod.POST, url, listener);
47 | }
48 |
49 | /**
50 | * @return
51 | */
52 | public MultipartEntity getMultiPartEntity() {
53 | return mMultiPartEntity;
54 | }
55 |
56 | @Override
57 | public String getBodyContentType() {
58 | return mMultiPartEntity.getContentType().getValue();
59 | }
60 |
61 | @Override
62 | public byte[] getBody() {
63 | ByteArrayOutputStream bos = new ByteArrayOutputStream();
64 | try {
65 | // 将MultipartEntity中的参数写入到bos中
66 | mMultiPartEntity.writeTo(bos);
67 | } catch (IOException e) {
68 | Log.e("", "IOException writing to ByteArrayOutputStream");
69 | }
70 | return bos.toByteArray();
71 | }
72 |
73 | @Override
74 | public String parseResponse(Response response) {
75 | if (response != null && response.getRawData() != null) {
76 | return new String(response.getRawData());
77 | }
78 |
79 | return "";
80 | }
81 |
82 | }
83 |
--------------------------------------------------------------------------------
/network/SimpleNet/src/org/simple/net/requests/StringRequest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 bboyfeiyu@gmail.com, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.net.requests;
26 |
27 | import org.simple.net.base.Request;
28 | import org.simple.net.base.Response;
29 |
30 | public class StringRequest extends Request {
31 |
32 | public StringRequest(HttpMethod method, String url, RequestListener listener) {
33 | super(method, url, listener);
34 | }
35 |
36 | @Override
37 | public String parseResponse(Response response) {
38 | return new String(response.getRawData());
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/orm/README.md:
--------------------------------------------------------------------------------
1 | # 任务表
2 | | 开源框架名称 | 作者 | 预计完成时间 |
3 | | ------------- |:-------------:| ------------- |
4 | | | [用户名](git地址) | 完成时间 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/others/README.md:
--------------------------------------------------------------------------------
1 | # 任务表
2 | | 简版框架名称 | 作者 | 预计完成时间 |
3 | | ------------- |:-------------:| ------------- |
4 | | 这里指向你的框架文件夹 | [用户名](git地址) | 完成时间 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/template.md:
--------------------------------------------------------------------------------
1 | ${简版开源库} 的设计与实现
2 | ====================================
3 | > 本文为 [Android著名开源库的简版实现](https://github.com/simple-android-framework-exchange/simple-android-opensource-framework) 中的 ${简版开源库} 的设计与实现
4 | > 原始开源库: [${原始开源库名称}](链接)
5 | > 作者:[作者用户名](作者github链接),开发状态:完成/未完成,校对者:[等待管理员填写](),校对状态:未开始
6 |
7 |
8 | 建议大家看下 [SimpleNet](network/SimpleNet/README.md),了解应该写到什么程度,以及类似流程图和总体设计该怎么做。当然,如果你只想写README.md一篇介绍,那么该文档中必须包含详细的设计与源码实现介绍,阐述其核心原理。如果你的README.md只是一篇介绍性文档,那么后续必须增加其他阐述核心原理的其他文档。
9 |
10 | `复制一份到自己的项目文件夹下,然后根据自己项目替换掉 ${} 内容,删掉本行及上面两行。`
11 |
12 | ## 1. 功能介绍
13 | 功能介绍,包括功能或优点等
14 |
15 |
16 | ## 2. 总体设计
17 | 整个库分为哪些模块及模块之间的调用关系。
18 | - 如大多数图片缓存会分为 Loader 和 Processer 等模块。
19 | - 可使用 [Google Drawing](https://docs.google.com/drawings)、[Visio](http://products.office.com/en-us/visio/flowchart-software)、[StarUML](http://staruml.io/) 等工具完成,其他工具推荐??
20 | - 非所有项目必须,不需要的请先在群里反馈。
21 |
22 |
23 | ## 3. 流程图
24 | 主要功能流程图
25 |
26 | - 如 Retrofit、Volley 的请求处理流程,Android-Universal-Image-Loader 的图片处理流程图
27 | - 可使用 [Google Drawing](https://docs.google.com/drawings)、[Visio](http://products.office.com/en-us/visio/flowchart-software)、[StarUML](http://staruml.io/) 等工具完成,其他工具推荐??
28 | - 非所有项目必须,不需要的请先在群里反馈
29 |
30 |
31 |
32 |
33 | ## 4. 详细设计
34 | ### 4.1 核心类详细介绍
35 |
36 | 类及其主要函数功能介绍、核心功能流程图,流程图可使用 [Google Drawing](https://docs.google.com/drawings)、[Visio](http://products.office.com/en-us/visio/flowchart-software)、[StarUML](http://staruml.io/)。
37 |
38 |
39 | ### 4.2 类关系图
40 | 类关系图,类的继承、组合关系图,可是用 [StarUML](http://staruml.io/) 工具。
41 |
42 |
43 |
44 |
45 | ##5. 杂谈
46 | 该项目存在的问题、可优化点及类似功能项目对比等,非所有项目必须。
47 |
48 |
49 |
50 |
51 | `写完相关内容之后到开发群告知管理员,管理员安排相关人员进行审核,审核通过之后即可。`
52 |
53 |
--------------------------------------------------------------------------------
/view/README.md:
--------------------------------------------------------------------------------
1 | # 任务表
2 | | 简版框架名称 | 作者 | 预计完成时间 |
3 | | ------------- |:-------------:| ------------- |
4 | | 这里指向你的框架文件夹 | [用户名](git地址) | 完成时间 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------