Skip to content

Commit 40c06f7

Browse files
author
jordanqin
committed
update qcloud sdk to 1.5.49
1 parent 04dd41e commit 40c06f7

File tree

7 files changed

+54
-27
lines changed

7 files changed

+54
-27
lines changed

QCloudFoundation/foundation/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
minSdkVersion 15
88
targetSdkVersion 28
99

10-
versionCode 10548
11-
versionName "1.5.48"
10+
versionCode 10549
11+
versionName "1.5.49"
1212

1313
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
1414

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/HttpRequest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ public void addHeader(String name, String value) {
124124
}
125125
}
126126

127+
public void addOrReplaceHeader(String name, String value) {
128+
List<String> values = headers.get(name);
129+
if (values != null && values.size() > 0) {
130+
headers.remove(name);
131+
requestBuilder.removeHeader(name);
132+
removeHeaderNameValue(headers, name);
133+
}
134+
135+
requestBuilder.addHeader(name, value);
136+
addHeaderNameValue(headers, name, value);
137+
}
138+
127139
public void setUrl(String url){
128140
requestBuilder.url(url);
129141
}
@@ -201,6 +213,10 @@ private static void addHeaderNameValue(Map<String, List<String>> headers, String
201213
values.add(value.trim());
202214
}
203215

216+
private static void removeHeaderNameValue(Map<String, List<String>> headers, String name) {
217+
headers.remove(name);
218+
}
219+
204220
public static class Builder<T> {
205221
Object tag;
206222

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/HttpTaskMetrics.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public class HttpTaskMetrics {
7070
@Nullable String domainName;
7171
@Nullable List<InetAddress> remoteAddress;
7272
@Nullable InetAddress connectAddress;
73+
private int retryCount;
7374

7475
void onTaskStart() {
7576
fullTaskStartTime = System.nanoTime();
@@ -235,6 +236,14 @@ public void setDomainName(@Nullable String domainName) {
235236
this.domainName = domainName;
236237
}
237238

239+
public int getRetryCount() {
240+
return retryCount;
241+
}
242+
243+
public void setRetryCount(int retryCount) {
244+
this.retryCount = retryCount;
245+
}
246+
238247
private double toSeconds(long nanotime) {
239248
return (double)nanotime / 1_000_000_000.0;
240249
}
@@ -289,6 +298,7 @@ synchronized public HttpTaskMetrics merge(HttpTaskMetrics taskMetrics) {
289298
if (taskMetrics.connectAddress != null) {
290299
connectAddress = taskMetrics.getConnectAddress();
291300
}
301+
retryCount += taskMetrics.retryCount;
292302
return this;
293303
}
294304

@@ -297,6 +307,7 @@ synchronized public HttpTaskMetrics merge(HttpTaskMetrics taskMetrics) {
297307
public String toString() {
298308
return new StringBuilder().append("Http Metrics: \n")
299309
.append("domain : ").append(domainName).append("\n")
310+
.append("retryCount : ").append(retryCount).append("\n")
300311
.append("dns : ").append(connectAddress != null ? connectAddress.getHostAddress() : "null").append("\n")
301312
.append("fullTaskTookTime : ").append(fullTaskTookTime()).append("\n")
302313
.append("calculateMD5STookTime : ").append(calculateMD5STookTime()).append("\n")

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/OkHttpClientImpl.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
package com.tencent.qcloud.core.http;
2424

25+
import com.tencent.qcloud.core.BuildConfig;
2526
import com.tencent.qcloud.core.http.interceptor.HttpMetricsInterceptor;
2627
import com.tencent.qcloud.core.http.interceptor.RetryInterceptor;
2728
import com.tencent.qcloud.core.http.interceptor.TrafficControlInterceptor;
@@ -31,7 +32,6 @@
3132
import javax.net.ssl.HostnameVerifier;
3233

3334
import okhttp3.Call;
34-
import okhttp3.ConnectionPool;
3535
import okhttp3.Dns;
3636
import okhttp3.OkHttpClient;
3737

@@ -51,7 +51,11 @@ public void init(QCloudHttpClient.Builder b, HostnameVerifier hostnameVerifier,
5151
final Dns dns, HttpLogger httpLogger) {
5252
super.init(b, hostnameVerifier, dns, httpLogger);
5353
HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(httpLogger);
54-
logInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
54+
if(BuildConfig.DEBUG){
55+
logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
56+
} else {
57+
logInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
58+
}
5559
OkHttpClient.Builder builder = b.mBuilder;
5660
okHttpClient = builder
5761
.followRedirects(true)

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/StreamingRequestBody.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,12 @@
2323
package com.tencent.qcloud.core.http;
2424

2525
import android.content.ContentResolver;
26-
import android.content.Context;
2726
import android.net.Uri;
28-
import android.os.Environment;
2927

3028
import com.tencent.qcloud.core.common.QCloudDigistListener;
3129
import com.tencent.qcloud.core.common.QCloudProgressListener;
3230
import com.tencent.qcloud.core.logger.QCloudLogger;
3331
import com.tencent.qcloud.core.util.Base64Utils;
34-
import com.tencent.qcloud.core.util.ContextHolder;
3532
import com.tencent.qcloud.core.util.QCloudUtils;
3633

3734
import java.io.ByteArrayInputStream;
@@ -179,7 +176,7 @@ protected long getContentRawLength() throws IOException {
179176
} else if (bytes != null) {
180177
contentRawLength = bytes.length;
181178
} else if (uri != null) {
182-
contentRawLength = QCloudUtils.getUriContentLength2(uri, contentResolver);
179+
contentRawLength = QCloudUtils.getUriContentLength(uri, contentResolver);
183180
}
184181
}
185182

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/interceptor/RetryInterceptor.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.tencent.qcloud.core.http.HttpConfiguration;
3232
import com.tencent.qcloud.core.http.HttpConstants;
3333
import com.tencent.qcloud.core.http.HttpTask;
34+
import com.tencent.qcloud.core.http.HttpTaskMetrics;
3435
import com.tencent.qcloud.core.http.QCloudHttpRetryHandler;
3536
import com.tencent.qcloud.core.logger.QCloudLogger;
3637
import com.tencent.qcloud.core.task.RetryStrategy;
@@ -174,6 +175,12 @@ Response processRequest(Chain chain, Request request, HttpTask task) throws IOEx
174175
}
175176
QCloudLogger.i(HTTP_LOG_TAG, "%s start to execute, attempts is %d", request, attempts);
176177

178+
//记录重试次数
179+
HttpTaskMetrics metrics = task.metrics();
180+
if (metrics != null) {
181+
metrics.setRetryCount(attempts);
182+
}
183+
177184
attempts++;
178185
int statusCode = -1;
179186
try {
@@ -209,6 +216,10 @@ Response processRequest(Chain chain, Request request, HttpTask task) throws IOEx
209216
} else if (shouldRetry(request, response, attempts, task.getWeight(), startTime, e, statusCode) && !task.isCanceled()) {
210217
QCloudLogger.i(HTTP_LOG_TAG, "%s failed for %s, code is %d", request, e, statusCode);
211218
retryStrategy.onTaskEnd(false, e);
219+
//解决okhttp 3.14 以上版本报错 cannot make a new request because the previous response is still open: please call response.close()
220+
// if (response != null) {
221+
// Util.closeQuietly(response.body());
222+
// }
212223
} else {
213224
QCloudLogger.i(HTTP_LOG_TAG, "%s ends for %s, code is %d", request, e, statusCode);
214225
break;

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/util/QCloudUtils.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,38 +56,26 @@ public static boolean isNotEmpty(String str) {
5656
public static long getUriContentLength(Uri uri, ContentResolver contentResolver) {
5757
String scheme = uri.getScheme();
5858
if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
59+
long length = -1;
5960
Cursor cursor = contentResolver.query(uri,
6061
null, null, null, null);
6162
if (cursor != null) {
6263
try {
6364
int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
6465
if (cursor.moveToFirst()) {
65-
return cursor.getLong(sizeIndex);
66+
length = cursor.getLong(sizeIndex);
6667
}
67-
return -1;
6868
} finally {
6969
cursor.close();
7070
}
7171
}
72-
} else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
73-
File file = new File(uri.getPath());
74-
return file.length();
75-
}
7672

77-
return -1;
78-
}
73+
//适配部分 vivo 机型
74+
if(length == -1){
75+
length = getUriContentLengthByRead(uri, contentResolver);
76+
}
7977

80-
/**
81-
* 部分 vivo 机型
82-
*
83-
* @param uri
84-
* @param contentResolver
85-
* @return
86-
*/
87-
public static long getUriContentLength2(Uri uri, ContentResolver contentResolver) {
88-
String scheme = uri.getScheme();
89-
if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
90-
return getUriContentLengthByRead(uri, contentResolver);
78+
return length;
9179
} else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
9280
File file = new File(uri.getPath());
9381
return file.length();

0 commit comments

Comments
 (0)