Skip to content

Commit be1936d

Browse files
authored
Merge pull request #346 from hamberluo/feature/support_okhttp4
支持 okhttp4
2 parents 4343ced + cba68e2 commit be1936d

File tree

9 files changed

+137
-19
lines changed

9 files changed

+137
-19
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ https://github.com/qiniudemo/qiniu-lab-android
1414

1515
| Qiniu SDK 版本 | 最低 Android版本 | 依赖库版本 |
1616
|------------ |-----------------|------------------------|
17+
| 7.5.x | Android 4.0+ | okhttp 4+ |
1718
| 7.2.x | Android 2.3+ | okhttp 3+ |
1819
| 7.1.x | Android 2.3+ | okhttp 2.6+ |
1920
| 7.0.8,7.0.9 | Android 2.2+ | android-async-http 1.4.9 |

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
buildscript {
77
repositories {
88
jcenter()
9+
google()
910
}
1011
dependencies {
11-
classpath 'com.android.tools.build:gradle:2.1.0'
12+
classpath 'com.android.tools.build:gradle:3.5.1'
1213

1314
// NOTE: Do not place your application dependencies here; they belong
1415
// in the individual module build.gradle files
@@ -18,5 +19,6 @@ buildscript {
1819
allprojects {
1920
repositories {
2021
jcenter()
22+
google()
2123
}
2224
}

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.14-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.3-all.zip

library/build.gradle

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ String version = versionName()
2020
int code = versionNameToCode(version)
2121

2222
android {
23-
compileSdkVersion 26
24-
buildToolsVersion '25.0.2'
23+
compileSdkVersion 29
24+
buildToolsVersion '29.0.2'
2525
defaultConfig {
2626
//applicationId "com.qiniu.android"
27-
minSdkVersion 9
27+
minSdkVersion 15
2828
targetSdkVersion 26
2929
versionCode code
3030
versionName version
@@ -43,13 +43,18 @@ android {
4343
lintOptions {
4444
warning 'InvalidPackage'
4545
}
46+
47+
useLibrary 'android.test.runner'
48+
useLibrary 'android.test.base'
49+
useLibrary 'android.test.mock'
4650
}
4751

4852
dependencies {
49-
compile 'com.squareup.okhttp3:okhttp:3.11.0'
53+
implementation 'com.squareup.okhttp3:okhttp:4.2.2'
5054
// for javax.annotation.Nullable use in custom MultipartBody and Headers implements.
51-
// compile 'com.google.code.findbugs:jsr305:3.0.2'
52-
compile fileTree(include: ['*.jar'], dir: 'libs')
55+
// implementation 'com.google.code.findbugs:jsr305:3.0.2'
56+
implementation fileTree(include: ['*.jar'], dir: 'libs')
57+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
5358
}
5459

5560
task releaseJar(type: Jar, dependsOn: 'build') {
@@ -62,7 +67,7 @@ task releaseJar(type: Jar, dependsOn: 'build') {
6267
//去掉不需要打包的目录和文件
6368
exclude('test/', 'BuildConfig.class', 'R.class')
6469
//去掉R$开头的文件
65-
exclude { it.name.startsWith('R$'); }
70+
exclude { it.name.startsWith('R$') }
6671
}
6772

6873
android.libraryVariants.all { variant ->
@@ -71,7 +76,7 @@ android.libraryVariants.all { variant ->
7176
task.dependsOn variant.javaCompile
7277
task.from variant.javaCompile.destinationDir
7378
task.exclude '**/R.*', '**/R$*.*', '**/BuildConfig.class'
74-
artifacts.add('archives', task);
79+
artifacts.add('archives', task)
7580
}
7681

7782
setProperty('VERSION_NAME', version)
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
<manifest package="com.qiniu.android"
2-
xmlns:android="http://schemas.android.com/apk/res/android">
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.qiniu.android">
33

44
<uses-permission android:name="android.permission.INTERNET" />
55
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
66
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
77

8-
<application android:label="@string/app_name"></application>
8+
<application android:label="@string/app_name">
9+
<uses-library
10+
android:name="android.test.runner"
11+
android:required="false" />
12+
</application>
913

1014
</manifest>

library/src/main/java/com/qiniu/android/common/Constants.java

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

33

44
public final class Constants {
5-
public static final String VERSION = "7.4.2";
5+
public static final String VERSION = "7.5.0";
66

77
public static final String UTF_8 = "utf-8";
88
}

library/src/main/java/com/qiniu/android/http/Headers.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
package com.qiniu.android.http;
2323

24+
import com.qiniu.android.utils.HttpDate;
25+
2426
import java.util.ArrayList;
2527
import java.util.Arrays;
2628
import java.util.Collections;
@@ -33,7 +35,6 @@
3335
import java.util.TreeSet;
3436

3537
import okhttp3.internal.Util;
36-
import okhttp3.internal.http.HttpDate;
3738

3839
/**
3940
* The header fields of a single HTTP message. Values are uninterpreted strings; use {@code Request}
@@ -137,8 +138,7 @@ public String get(String name) {
137138
* either the field is absent or cannot be parsed as a date.
138139
*/
139140
public Date getDate(String name) {
140-
String value = get(name);
141-
return value != null ? HttpDate.parse(value) : null;
141+
return HttpDate.parse(get(name));
142142
}
143143

144144
/** Returns the number of field values. */

library/src/main/java/com/qiniu/android/http/MultipartBody.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222

2323
import java.io.IOException;
2424
import java.util.ArrayList;
25+
import java.util.Collections;
2526
import java.util.List;
2627
import java.util.UUID;
28+
2729
import okhttp3.MediaType;
2830
import okhttp3.RequestBody;
29-
import okhttp3.internal.Util;
3031
import okio.Buffer;
3132
import okio.BufferedSink;
3233
import okio.ByteString;
@@ -81,7 +82,7 @@ public final class MultipartBody extends RequestBody {
8182
this.boundary = boundary;
8283
this.originalType = type;
8384
this.contentType = MediaType.get(type + "; boundary=" + boundary.utf8());
84-
this.parts = Util.immutableList(parts);
85+
this.parts = Collections.unmodifiableList(parts);
8586
}
8687

8788
/**
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.qiniu.android.utils;
2+
3+
import android.text.TextUtils;
4+
5+
import java.text.DateFormat;
6+
import java.text.ParsePosition;
7+
import java.text.SimpleDateFormat;
8+
import java.util.Date;
9+
import java.util.Locale;
10+
11+
import static okhttp3.internal.Util.UTC;
12+
13+
/**
14+
* Best-effort parser for HTTP dates.
15+
*/
16+
public final class HttpDate {
17+
18+
/**
19+
* Most websites serve cookies in the blessed format. Eagerly create the parser to ensure such
20+
* cookies are on the fast path.
21+
*/
22+
private static final ThreadLocal<DateFormat> STANDARD_DATE_FORMAT =
23+
new ThreadLocal<DateFormat>() {
24+
@Override
25+
protected DateFormat initialValue() {
26+
// Date format specified by RFC 7231 section 7.1.1.1.
27+
DateFormat rfc1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
28+
rfc1123.setLenient(false);
29+
rfc1123.setTimeZone(UTC);
30+
return rfc1123;
31+
}
32+
};
33+
34+
/**
35+
* If we fail to parse a date in a non-standard format, try each of these formats in sequence.
36+
*/
37+
private static final String[] BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS = new String[]{
38+
// HTTP formats required by RFC2616 but with any timezone.
39+
"EEE, dd MMM yyyy HH:mm:ss zzz", // RFC 822, updated by RFC 1123 with any TZ
40+
"EEEE, dd-MMM-yy HH:mm:ss zzz", // RFC 850, obsoleted by RFC 1036 with any TZ.
41+
"EEE MMM d HH:mm:ss yyyy", // ANSI C's asctime() format
42+
// Alternative formats.
43+
"EEE, dd-MMM-yyyy HH:mm:ss z",
44+
"EEE, dd-MMM-yyyy HH-mm-ss z",
45+
"EEE, dd MMM yy HH:mm:ss z",
46+
"EEE dd-MMM-yyyy HH:mm:ss z",
47+
"EEE dd MMM yyyy HH:mm:ss z",
48+
"EEE dd-MMM-yyyy HH-mm-ss z",
49+
"EEE dd-MMM-yy HH:mm:ss z",
50+
"EEE dd MMM yy HH:mm:ss z",
51+
"EEE,dd-MMM-yy HH:mm:ss z",
52+
"EEE,dd-MMM-yyyy HH:mm:ss z",
53+
"EEE, dd-MM-yyyy HH:mm:ss z",
54+
55+
/* RI bug 6641315 claims a cookie of this format was once served by www.yahoo.com */
56+
"EEE MMM d yyyy HH:mm:ss z",
57+
};
58+
59+
private static final DateFormat[] BROWSER_COMPATIBLE_DATE_FORMATS =
60+
new DateFormat[BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS.length];
61+
62+
/**
63+
* Returns the date for {@code value}. Returns null if the value couldn't be parsed.
64+
*/
65+
public static Date parse(String value) {
66+
if (TextUtils.isEmpty(value)) {
67+
return null;
68+
}
69+
70+
ParsePosition position = new ParsePosition(0);
71+
Date result = STANDARD_DATE_FORMAT.get().parse(value, position);
72+
if (position.getIndex() == value.length()) {
73+
// STANDARD_DATE_FORMAT must match exactly; all text must be consumed, e.g. no ignored
74+
// non-standard trailing "+01:00". Those cases are covered below.
75+
return result;
76+
}
77+
synchronized (BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS) {
78+
for (int i = 0, count = BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS.length; i < count; i++) {
79+
DateFormat format = BROWSER_COMPATIBLE_DATE_FORMATS[i];
80+
if (format == null) {
81+
format = new SimpleDateFormat(BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS[i], Locale.US);
82+
// Set the timezone to use when interpreting formats that don't have a timezone. GMT is
83+
// specified by RFC 7231.
84+
format.setTimeZone(UTC);
85+
BROWSER_COMPATIBLE_DATE_FORMATS[i] = format;
86+
}
87+
position.setIndex(0);
88+
result = format.parse(value, position);
89+
if (position.getIndex() != 0) {
90+
// Something was parsed. It's possible the entire string was not consumed but we ignore
91+
// that. If any of the BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS ended in "'GMT'" we'd have
92+
// to also check that position.getIndex() == value.length() otherwise parsing might have
93+
// terminated early, ignoring things like "+01:00". Leaving this as != 0 means that any
94+
// trailing junk is ignored.
95+
return result;
96+
}
97+
}
98+
}
99+
return null;
100+
}
101+
102+
private HttpDate() {
103+
}
104+
}
105+

0 commit comments

Comments
 (0)