Skip to content

Commit b7bc4dd

Browse files
committed
Merge pull request #200 from longbai/okhttp3
up to okhttp3
2 parents 1144aca + 098b530 commit b7bc4dd

File tree

11 files changed

+106
-90
lines changed

11 files changed

+106
-90
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ sudo: false
44
language: java
55

66
jdk:
7-
- openjdk6
7+
# - openjdk6
88
- oraclejdk7
99
- oraclejdk8
1010

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#Changelog
22

3+
## 7.1.0 (2016-04-27)
4+
### 增加
5+
* 升级到okhttp3, 升级次版本号。
6+
37
## 7.0.9 (2016-04-22)
48
### 增加
59
* 强制copy或者move

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,22 @@
1010
<dependency>
1111
<groupId>com.qiniu</groupId>
1212
<artifactId>qiniu-java-sdk</artifactId>
13-
<version>[7.0.0, 7.0.99]</version>
13+
<version>[7.0.0, 7.1.99]</version>
1414
</dependency>
1515
```
1616
或者 Gradle:
1717
```groovy
18-
compile 'com.qiniu:qiniu-java-sdk:7.0.+'
18+
compile 'com.qiniu:qiniu-java-sdk:7.1.+'
1919
```
20-
jdk 6.0 不能直接使用mvn上的okhttp, 需要另外下载,[代码][2], [okhttp.jar][3], [okio.jar][4]
20+
7.0.x 版本的jdk 6.0 不能直接使用mvn上的okhttp, 需要另外下载,[代码][2], [okhttp.jar][3], [okio.jar][4]
21+
7.1.x 版本 jdk6.0 支持 后面会做处理,暂时只支持7及以上。
2122

2223
## 运行环境
2324

2425
| Qiniu SDK版本 | Java 版本 |
2526
|:--------------------:|:---------------------------:|
26-
| 7.x | 6+ |
27+
| 7.1.x | 7+ |
28+
| 7.0.x | 6+ |
2729
| 6.x | 6+ |
2830

2931
## 使用方法

build.gradle

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import java.util.regex.Matcher
22

33
apply plugin: 'java'
44

5-
sourceCompatibility = 1.6
6-
targetCompatibility = 1.6
5+
sourceCompatibility = 1.7
6+
targetCompatibility = 1.7
77
version = '1.0'
88
[compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8'
99

@@ -12,19 +12,24 @@ repositories {
1212
}
1313

1414
dependencies {
15-
String version = System.getProperty("java.version")
16-
println("JDK: " + version)
17-
int c = version.compareTo("1.7")
18-
if (c < 0) {
19-
compile fileTree(dir: 'libs', include: '*.jar')
20-
} else {
21-
compile group:'com.squareup.okhttp', name:'okhttp', version:'2.7.1'
22-
}
23-
compile group:'com.google.code.gson', name:'gson', version:'2.3.1'
24-
testCompile group: 'junit', name: 'junit', version: '4.11'
15+
// String version = System.getProperty("java.version")
16+
// println("JDK: " + version)
17+
// int c = version.compareTo("1.7")
18+
// if (c < 0) {
19+
// compile fileTree(dir: 'libs', include: '*.jar')
20+
// } else {
21+
compile group:'com.squareup.okhttp3', name:'okhttp', version:'3.2.0'
22+
// }
23+
compile group:'com.google.code.gson', name:'gson', version:'2.6.2'
24+
testCompile group: 'junit', name: 'junit', version: '4.12'
2525
println("JDK : " + version)
2626
}
2727

28+
29+
task getHomeDir << {
30+
println gradle.gradleHomeDir
31+
}
32+
2833
apply plugin: 'checkstyle'
2934

3035

@@ -56,4 +61,4 @@ task gen_eclipse(dependsOn:[
5661
'cleanEclipseProject', 'cleanEclipseClasspath',
5762
'eclipseProject', 'eclipseClasspath'])
5863
eclipseProject.mustRunAfter cleanEclipseProject
59-
eclipseClasspath.mustRunAfter cleanEclipseClasspath
64+
eclipseClasspath.mustRunAfter cleanEclipseClasspath

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip

src/main/java/com/qiniu/common/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public final class Config {
88

9-
public static final String VERSION = "7.0.9";
9+
public static final String VERSION = "7.1.0";
1010
/**
1111
* 断点上传时的分块大小(默认的分块大小, 不允许改变)
1212
*/

src/main/java/com/qiniu/http/Client.java

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.qiniu.common.QiniuException;
55
import com.qiniu.util.StringMap;
66
import com.qiniu.util.StringUtils;
7-
import com.squareup.okhttp.*;
7+
import okhttp3.*;
88
import okio.BufferedSink;
99

1010
import java.io.File;
@@ -25,25 +25,31 @@ public Client() {
2525
Dispatcher dispatcher = new Dispatcher();
2626
dispatcher.setMaxRequests(64);
2727
dispatcher.setMaxRequestsPerHost(16);
28-
ConnectionPool connectionPool = new ConnectionPool(32, 5 * 60 * 1000);
29-
httpClient = new OkHttpClient();
30-
httpClient.setDispatcher(dispatcher);
31-
httpClient.setConnectionPool(connectionPool);
32-
httpClient.networkInterceptors().add(new Interceptor() {
28+
ConnectionPool connectionPool = new ConnectionPool(32, 5, TimeUnit.MINUTES);
29+
OkHttpClient.Builder builder = new OkHttpClient.Builder();
30+
31+
builder.dispatcher(dispatcher);
32+
builder.connectionPool(connectionPool);
33+
builder.addNetworkInterceptor(new Interceptor() {
3334
@Override
34-
public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
35+
public okhttp3.Response intercept(Chain chain) throws IOException {
3536
Request request = chain.request();
3637

37-
com.squareup.okhttp.Response response = chain.proceed(request);
38+
okhttp3.Response response = chain.proceed(request);
3839
IpTag tag = (IpTag) request.tag();
39-
String ip = chain.connection().getSocket().getRemoteSocketAddress().toString();
40-
tag.ip = ip;
40+
try {
41+
tag.ip = chain.connection().socket().getRemoteSocketAddress().toString();
42+
} catch (Exception e) {
43+
e.printStackTrace();
44+
tag.ip = "";
45+
}
4146
return response;
4247
}
4348
});
44-
httpClient.setConnectTimeout(Config.CONNECT_TIMEOUT, TimeUnit.SECONDS);
45-
httpClient.setReadTimeout(Config.RESPONSE_TIMEOUT, TimeUnit.SECONDS);
46-
httpClient.setWriteTimeout(Config.WRITE_TIMEOUT, TimeUnit.SECONDS);
49+
builder.connectTimeout(Config.CONNECT_TIMEOUT, TimeUnit.SECONDS);
50+
builder.readTimeout(Config.RESPONSE_TIMEOUT, TimeUnit.SECONDS);
51+
builder.writeTimeout(Config.WRITE_TIMEOUT, TimeUnit.SECONDS);
52+
httpClient = builder.build();
4753
}
4854

4955
private static String userAgent() {
@@ -94,7 +100,7 @@ public Response post(String url, String body, StringMap headers) throws QiniuExc
94100
}
95101

96102
public Response post(String url, StringMap params, StringMap headers) throws QiniuException {
97-
final FormEncodingBuilder f = new FormEncodingBuilder();
103+
final FormBody.Builder f = new FormBody.Builder();
98104
params.forEach(new StringMap.Consumer() {
99105
@Override
100106
public void accept(String key, Object value) {
@@ -160,7 +166,7 @@ private Response multipartPost(String url,
160166
String fileName,
161167
RequestBody file,
162168
StringMap headers) throws QiniuException {
163-
final MultipartBuilder mb = new MultipartBuilder();
169+
final MultipartBody.Builder mb = new MultipartBody.Builder();
164170
mb.addFormDataPart(name, fileName, file);
165171

166172
fields.forEach(new StringMap.Consumer() {
@@ -169,7 +175,7 @@ public void accept(String key, Object value) {
169175
mb.addFormDataPart(key, value.toString());
170176
}
171177
});
172-
mb.type(MediaType.parse("multipart/form-data"));
178+
mb.setType(MediaType.parse("multipart/form-data"));
173179
RequestBody body = mb.build();
174180
Request.Builder requestBuilder = new Request.Builder().url(url).post(body);
175181
return send(requestBuilder, headers);
@@ -187,7 +193,7 @@ public void accept(String key, Object value) {
187193

188194
requestBuilder.header("User-Agent", userAgent());
189195
long start = System.currentTimeMillis();
190-
com.squareup.okhttp.Response res = null;
196+
okhttp3.Response res = null;
191197
Response r;
192198
double duration = (System.currentTimeMillis() - start) / 1000.0;
193199
IpTag tag = new IpTag();
@@ -220,14 +226,14 @@ public void accept(String key, Object value) {
220226
IpTag tag = new IpTag();
221227
httpClient.newCall(requestBuilder.tag(tag).build()).enqueue(new Callback() {
222228
@Override
223-
public void onFailure(Request request, IOException e) {
229+
public void onFailure(Call call, IOException e) {
224230
e.printStackTrace();
225231
long duration = (System.currentTimeMillis() - start) / 1000;
226232
cb.complete(Response.createError(null, "", duration, e.getMessage()));
227233
}
228234

229235
@Override
230-
public void onResponse(com.squareup.okhttp.Response response) throws IOException {
236+
public void onResponse(Call call, okhttp3.Response response) throws IOException {
231237
long duration = (System.currentTimeMillis() - start) / 1000;
232238
cb.complete(Response.create(response, "", duration));
233239
}
@@ -279,7 +285,7 @@ private void asyncMultipartPost(String url,
279285
RequestBody file,
280286
StringMap headers,
281287
AsyncCallback cb) {
282-
final MultipartBuilder mb = new MultipartBuilder();
288+
final MultipartBody.Builder mb = new MultipartBody.Builder();
283289
mb.addFormDataPart(name, fileName, file);
284290

285291
fields.forEach(new StringMap.Consumer() {
@@ -288,7 +294,7 @@ public void accept(String key, Object value) {
288294
mb.addFormDataPart(key, value.toString());
289295
}
290296
});
291-
mb.type(MediaType.parse("multipart/form-data"));
297+
mb.setType(MediaType.parse("multipart/form-data"));
292298
RequestBody body = mb.build();
293299
Request.Builder requestBuilder = new Request.Builder().url(url).post(body);
294300
asyncSend(requestBuilder, headers, cb);

src/main/java/com/qiniu/http/Response.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.qiniu.util.Json;
66
import com.qiniu.util.StringMap;
77
import com.qiniu.util.StringUtils;
8-
import com.squareup.okhttp.MediaType;
8+
import okhttp3.MediaType;
99

1010
import java.io.IOException;
1111
import java.util.Locale;
@@ -48,9 +48,9 @@ public final class Response {
4848
public final String address;
4949

5050
private byte[] body;
51-
private com.squareup.okhttp.Response response;
51+
private okhttp3.Response response;
5252

53-
private Response(com.squareup.okhttp.Response response, int statusCode, String reqId, String xlog, String xvia,
53+
private Response(okhttp3.Response response, int statusCode, String reqId, String xlog, String xvia,
5454
String address, double duration, String error, byte[] body) {
5555
this.response = response;
5656
this.statusCode = statusCode;
@@ -63,7 +63,7 @@ private Response(com.squareup.okhttp.Response response, int statusCode, String r
6363
this.body = body;
6464
}
6565

66-
static Response create(com.squareup.okhttp.Response response, String address, double duration) {
66+
static Response create(okhttp3.Response response, String address, double duration) {
6767
String error = null;
6868
int code = response.code();
6969
String reqId = null;
@@ -88,7 +88,7 @@ static Response create(com.squareup.okhttp.Response response, String address, do
8888
address, duration, error, body);
8989
}
9090

91-
static Response createError(com.squareup.okhttp.Response response, String address, double duration, String error) {
91+
static Response createError(okhttp3.Response response, String address, double duration, String error) {
9292
if (response == null) {
9393
return new Response(null, -1, "", "", "", "", duration, error, null);
9494
}
@@ -116,7 +116,7 @@ static Response createError(com.squareup.okhttp.Response response, String addres
116116
}
117117

118118

119-
private static String via(com.squareup.okhttp.Response response) {
119+
private static String via(okhttp3.Response response) {
120120
String via;
121121
if (!(via = response.header("X-Via", "")).equals("")) {
122122
return via;
@@ -132,7 +132,7 @@ private static String via(com.squareup.okhttp.Response response) {
132132
return via;
133133
}
134134

135-
private static String ctype(com.squareup.okhttp.Response response) {
135+
private static String ctype(okhttp3.Response response) {
136136
MediaType mediaType = response.body().contentType();
137137
if (mediaType == null) {
138138
return "";
@@ -207,7 +207,7 @@ public boolean isJson() {
207207
}
208208

209209
public String url() {
210-
return response.request().urlString();
210+
return response.request().url().toString();
211211
}
212212

213213
public static class ErrorBody {

src/test/java/com/qiniu/storage/FormUploadTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,6 @@ public void testSizeMin2() {
244244
}
245245
}
246246

247-
class MyRet {
248-
public String hash;
249-
public String key;
250-
public String fsize;
251-
public String fname;
252-
public String mimeType;
253-
}
254-
255247
// @Test
256248
public void testFormLargeSize() {
257249
Config.PUT_THRESHOLD = 25 * 1024 * 1024;
@@ -277,7 +269,6 @@ public void testFormLargeSize() {
277269

278270
}
279271

280-
281272
// @Test
282273
public void testFormLargeSize2() {
283274
Config.PUT_THRESHOLD = 25 * 1024 * 1024;
@@ -308,4 +299,12 @@ public void testFormLargeSize2() {
308299

309300
}
310301

302+
class MyRet {
303+
public String hash;
304+
public String key;
305+
public String fsize;
306+
public String fname;
307+
public String mimeType;
308+
}
309+
311310
}

src/test/java/com/qiniu/storage/RecordUploadTest.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -171,30 +171,6 @@ public void test25M1k() throws Throwable {
171171
template(1024 * 25 + 1);
172172
}
173173

174-
class Up implements Callable<Response> {
175-
private final UploadManager uploadManager;
176-
private final File file;
177-
private final String key;
178-
private final String token;
179-
180-
public Up(UploadManager uploadManager, File file, String key, String token) {
181-
this.uploadManager = uploadManager;
182-
this.file = file;
183-
this.key = key;
184-
this.token = token;
185-
}
186-
187-
@Override
188-
public Response call() throws Exception {
189-
Response res = uploadManager.put(file, key, token);
190-
System.out.println("up: " + res);
191-
System.out.println("up: " + res.bodyString());
192-
isDone = true;
193-
response = res;
194-
return res;
195-
}
196-
}
197-
198174
@Test
199175
public void testLastModify() throws IOException {
200176
File f = File.createTempFile("qiniutest", "b");
@@ -240,4 +216,28 @@ public void testLastModify() throws IOException {
240216
long m4 = recoderFile.lastModified();
241217
assertTrue(m4 > m1);
242218
}
219+
220+
class Up implements Callable<Response> {
221+
private final UploadManager uploadManager;
222+
private final File file;
223+
private final String key;
224+
private final String token;
225+
226+
public Up(UploadManager uploadManager, File file, String key, String token) {
227+
this.uploadManager = uploadManager;
228+
this.file = file;
229+
this.key = key;
230+
this.token = token;
231+
}
232+
233+
@Override
234+
public Response call() throws Exception {
235+
Response res = uploadManager.put(file, key, token);
236+
System.out.println("up: " + res);
237+
System.out.println("up: " + res.bodyString());
238+
isDone = true;
239+
response = res;
240+
return res;
241+
}
242+
}
243243
}

0 commit comments

Comments
 (0)