Skip to content

Commit 242ff9a

Browse files
author
yanzhenjie
committed
Add NoHttp 4 OkHttp.
1 parent b3df530 commit 242ff9a

File tree

9 files changed

+58
-68
lines changed

9 files changed

+58
-68
lines changed

README-cn.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
[**English document**][1]
77

8-
技术交流群:547839514
9-
加群请一定阅读[群行为规范][2]
8+
如果你想用OkHttp作为NoHttp的底层库,请看这个项目:[NoHttp4OkHttp](https://github.com/yanzhenjie/NoHttp4OkHttp)
9+
10+
技术交流群:547839514,加群请一定阅读[群行为规范][2]
1011

1112
严振杰的主页:[http://www.yanzhenjie.com][12]
1213
严振杰的博客:[http://blog.yanzhenjie.com](http://blog.yanzhenjie.com)

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
[**中文版文档**](https://github.com/yanzhenjie/NoHttp/blob/master/README-cn.md)
77

8+
If you want to use OkHttp to do the bottom of the NoHttp, please see this project: [NoHttp4OkHttp](https://github.com/yanzhenjie/NoHttp4OkHttp).
9+
810
----
911
# NoHttp home page and doc url
1012
NoHttp source code: [https://github.com/yanzhenjie/NoHttp](https://github.com/yanzhenjie/NoHttp/)

nohttp/src/main/java/com/yolanda/nohttp/Connection.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class Connection implements Closeable {
5050
/**
5151
* Create a response.
5252
*
53+
* @param connection {@link HttpURLConnection}.
5354
* @param responseHeaders response headers.
5455
* @param inputStream According to the response code, the incoming data stream server.
5556
* @param exception Connection exceptions that occur in the process.

nohttp/src/main/java/com/yolanda/nohttp/rest/IRestConnection.java renamed to nohttp/src/main/java/com/yolanda/nohttp/IRestConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.yolanda.nohttp.rest;
16+
package com.yolanda.nohttp;
1717

1818
import com.yolanda.nohttp.Connection;
1919
import com.yolanda.nohttp.IBasicRequest;

nohttp/src/main/java/com/yolanda/nohttp/NoHttp.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import com.yolanda.nohttp.download.Downloader;
3030
import com.yolanda.nohttp.download.RestDownloadRequest;
3131
import com.yolanda.nohttp.rest.IParserRequest;
32-
import com.yolanda.nohttp.rest.IRestConnection;
3332
import com.yolanda.nohttp.rest.IRestParser;
3433
import com.yolanda.nohttp.rest.IRestProtocol;
3534
import com.yolanda.nohttp.rest.ImageRequest;
@@ -38,7 +37,6 @@
3837
import com.yolanda.nohttp.rest.Request;
3938
import com.yolanda.nohttp.rest.RequestQueue;
4039
import com.yolanda.nohttp.rest.Response;
41-
import com.yolanda.nohttp.rest.RestConnection;
4240
import com.yolanda.nohttp.rest.RestParser;
4341
import com.yolanda.nohttp.rest.RestProtocol;
4442
import com.yolanda.nohttp.rest.StringRequest;
@@ -366,7 +364,19 @@ public static DownloadQueue newDownloadQueue() {
366364
* @see #newDownloadQueue(Downloader, int)
367365
*/
368366
public static DownloadQueue newDownloadQueue(int threadPoolSize) {
369-
return newDownloadQueue(new DownloadConnection(), threadPoolSize);
367+
return newDownloadQueue(RestConnection.getInstance(), threadPoolSize);
368+
}
369+
370+
/**
371+
* Create a new download queue.
372+
*
373+
* @param threadPoolSize thread pool number, here is the number of concurrent tasks.
374+
* @return {@link DownloadQueue}.
375+
* @see #newDownloadQueue()
376+
* @see #newDownloadQueue(Downloader, int)
377+
*/
378+
public static DownloadQueue newDownloadQueue(IRestConnection iRestConnection, int threadPoolSize) {
379+
return newDownloadQueue(DownloadConnection.getInstance(iRestConnection), threadPoolSize);
370380
}
371381

372382
/**

nohttp/src/main/java/com/yolanda/nohttp/BasicConnection.java renamed to nohttp/src/main/java/com/yolanda/nohttp/RestConnection.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 Yan Zhenjie
2+
* Copyright © Yan Zhenjie. All Rights Reserved
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,21 +52,30 @@
5252
import javax.net.ssl.SSLSocketFactory;
5353

5454
/**
55-
* <p>
56-
* Package good Http implementation class, establish connection, read and write data.
57-
* </p>
58-
* Created in Aug 4, 2015 10:12:38 AM.
59-
*
60-
* @author Yan Zhenjie.
55+
* Created by Yan Zhenjie on 2016/9/4.
6156
*/
62-
public class BasicConnection {
57+
public class RestConnection implements IRestConnection {
58+
59+
private static IRestConnection instance;
60+
61+
public static IRestConnection getInstance() {
62+
synchronized (RestConnection.class) {
63+
if (instance == null)
64+
instance = new RestConnection();
65+
return instance;
66+
}
67+
}
68+
69+
private RestConnection() {
70+
}
6371

6472
/**
6573
* Send the request, send only head, parameters, such as file information.
6674
*
6775
* @param request {@link IBasicRequest}.
6876
* @return {@link ProtocolResult}.
6977
*/
78+
@Override
7079
public Connection getConnection(IBasicRequest request) {
7180
Logger.d("--------------Request start--------------");
7281

nohttp/src/main/java/com/yolanda/nohttp/download/DownloadConnection.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import android.text.TextUtils;
1919
import android.util.Log;
2020

21-
import com.yolanda.nohttp.BasicConnection;
2221
import com.yolanda.nohttp.Connection;
2322
import com.yolanda.nohttp.Headers;
2423
import com.yolanda.nohttp.Logger;
@@ -30,6 +29,7 @@
3029
import com.yolanda.nohttp.error.TimeoutError;
3130
import com.yolanda.nohttp.error.URLError;
3231
import com.yolanda.nohttp.error.UnKnownHostError;
32+
import com.yolanda.nohttp.IRestConnection;
3333
import com.yolanda.nohttp.tools.HeaderUtil;
3434
import com.yolanda.nohttp.tools.IOUtils;
3535
import com.yolanda.nohttp.tools.NetUtil;
@@ -51,9 +51,23 @@
5151
*
5252
* @author Yan Zhenjie.
5353
*/
54-
public class DownloadConnection extends BasicConnection implements Downloader {
54+
public class DownloadConnection implements Downloader {
5555

56-
public DownloadConnection() {
56+
private IRestConnection iRestConnection;
57+
58+
private static Downloader instance;
59+
60+
public static Downloader getInstance(IRestConnection iRestConnection) {
61+
synchronized (DownloadConnection.class) {
62+
if (instance == null) {
63+
instance = new DownloadConnection(iRestConnection);
64+
}
65+
return instance;
66+
}
67+
}
68+
69+
private DownloadConnection(IRestConnection iRestConnection) {
70+
this.iRestConnection = iRestConnection;
5771
}
5872

5973
@Override
@@ -105,7 +119,7 @@ public void download(int what, DownloadRequest downloadRequest, DownloadListener
105119
}
106120

107121
// 连接服务器。
108-
connection = getConnection(downloadRequest);
122+
connection = iRestConnection.getConnection(downloadRequest);
109123
Exception exception = connection.exception();
110124
if (exception != null)
111125
throw exception;

nohttp/src/main/java/com/yolanda/nohttp/rest/RestConnection.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

nohttp/src/main/java/com/yolanda/nohttp/rest/RestProtocol.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
*/
1616
package com.yolanda.nohttp.rest;
1717

18-
import com.yolanda.nohttp.BasicConnection;
1918
import com.yolanda.nohttp.Connection;
2019
import com.yolanda.nohttp.Headers;
20+
import com.yolanda.nohttp.IRestConnection;
21+
import com.yolanda.nohttp.RestConnection;
2122
import com.yolanda.nohttp.cache.Cache;
2223
import com.yolanda.nohttp.cache.CacheEntity;
2324
import com.yolanda.nohttp.error.NotFoundCacheError;
@@ -135,7 +136,7 @@ private ProtocolResult getHttpResponse(IProtocolRequest request) {
135136
Headers responseHeaders = connection.responseHeaders();
136137
Exception exception = connection.exception();
137138
if (exception == null) {
138-
if (BasicConnection.hasResponseBody(request.getRequestMethod(), responseHeaders.getResponseCode()))
139+
if (RestConnection.hasResponseBody(request.getRequestMethod(), responseHeaders.getResponseCode()))
139140
try {
140141
responseBody = IOUtils.toByteArray(connection.serverStream());
141142
} catch (IOException e) {// IOException.

0 commit comments

Comments
 (0)