Skip to content

Commit 5674410

Browse files
committed
1.2.6 update
1 parent 3760fb9 commit 5674410

File tree

7 files changed

+153
-35
lines changed

7 files changed

+153
-35
lines changed

BaseOkHttpX/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ android {
77
defaultConfig {
88
minSdk 21
99
versionCode 7
10-
versionName "1.2.5"
10+
versionName "1.2.6"
1111

1212
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1313
consumerProguardFiles "consumer-rules.pro"

BaseOkHttpX/src/main/java/com/kongzue/baseokhttp/x/interfaces/DownloadListener.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.kongzue.baseokhttp.x.util.BaseHttpRequest;
44

5+
import org.jetbrains.annotations.Nullable;
6+
57
import java.io.File;
68

79
/**
@@ -18,6 +20,7 @@ public interface DownloadListener {
1820
* @param current 已完成字节数
1921
* @param total 文件总大小
2022
* @param done 是否下载完成
23+
* @param error 异常信息
2124
*/
22-
void onDownload(BaseHttpRequest httpRequest, File downloadFile, float progress, long current, long total, boolean done);
25+
void onDownload(BaseHttpRequest httpRequest, File downloadFile, float progress, long current, long total, boolean done, @Nullable Exception error);
2326
}

BaseOkHttpX/src/main/java/com/kongzue/baseokhttp/x/interfaces/UploadListener.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.kongzue.baseokhttp.x.util.BaseHttpRequest;
44

5+
import org.jetbrains.annotations.Nullable;
6+
57
/**
68
* 上传进度回调接口。
79
*/
@@ -15,6 +17,7 @@ public interface UploadListener {
1517
* @param current 已上传字节数
1618
* @param total 总字节数
1719
* @param done 是否完成
20+
* @param error 异常信息
1821
*/
19-
void onUpload(BaseHttpRequest httpRequest, float progress, long current, long total, boolean done);
22+
void onUpload(BaseHttpRequest httpRequest, float progress, long current, long total, boolean done, @Nullable Exception error);
2023
}

BaseOkHttpX/src/main/java/com/kongzue/baseokhttp/x/util/BaseHttpRequest.java

Lines changed: 102 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ public void go() {
155155
responseBytes = null;
156156
responseMediaType = null;
157157
responseException = null;
158+
updateProgressCurrent = 0;
159+
updateProgressTotal = 0;
160+
updateProgressDone = false;
161+
downloadProgress = 0;
162+
downloadProgressCurrent = 0;
163+
downloadProgressTotal = 0;
158164
if (isShowLogs()) {
159165
LockLog.Builder logBuilder = LockLog.Builder.create()
160166
.i(TAG_SEND, "-------------------------------------")
@@ -355,7 +361,7 @@ public void run() {
355361
while ((len = is.read(buf)) != -1) {
356362
fos.write(buf, 0, len);
357363
sum += len;
358-
callDownloadingCallback(sum * 1.0f / total, sum, total);
364+
callDownloadingCallback(sum * 1.0f / total, sum, total, null);
359365
}
360366
fos.flush();
361367
}
@@ -371,7 +377,7 @@ private void callCallbacks() {
371377
return;
372378
}
373379
for (BaseResponseListener callback : callbacks) {
374-
callback.response(BaseHttpRequest.this, (responseBytes == null || responseMediaType == null) ? null :ResponseBody.create(responseBytes, responseMediaType), responseException);
380+
callback.response(BaseHttpRequest.this, (responseBytes == null || responseMediaType == null) ? null : ResponseBody.create(responseBytes, responseMediaType), responseException);
375381
}
376382
}
377383

@@ -421,6 +427,8 @@ public void run() {
421427
callCallbacks();
422428
}
423429
}
430+
callUploadingCallback(updateProgressCurrent, updateProgressTotal, updateProgressDone, e);
431+
callDownloadingCallback(downloadProgress, downloadProgressCurrent, downloadProgressTotal, e);
424432
}
425433

426434
private OkHttpClient createClient() {
@@ -479,7 +487,7 @@ private Request createRequest() {
479487
RequestBody requestBody = originRequestBody == null ? null : new RequestBodyImpl(originRequestBody) {
480488
@Override
481489
public void onUploading(long current, long total, boolean done) {
482-
callUploadingCallback(current, total, done);
490+
callUploadingCallback(current, total, done, null);
483491
}
484492
};
485493
switch (requestType) {
@@ -510,55 +518,73 @@ public void onUploading(long current, long total, boolean done) {
510518
return builder.build();
511519
}
512520

513-
private void callUploadingCallback(long current, long total, boolean done) {
521+
long updateProgressCurrent;
522+
long updateProgressTotal;
523+
boolean updateProgressDone;
524+
525+
private void callUploadingCallback(long current, long total, boolean done, Exception error) {
514526
if (getUploadListener() == null) return;
515527
if (callbackInMainLooper) {
516528
Looper mainLooper = Looper.getMainLooper();
517529
handler = new Handler(mainLooper);
518530
handler.post(new Runnable() {
519531
@Override
520532
public void run() {
521-
getUploadListener().onUpload(BaseHttpRequest.this, (float) current / total, current, total, done);
533+
getUploadListener().onUpload(BaseHttpRequest.this, (float) current / total, current, total, done, error);
522534
}
523535
});
524536
} else {
525537
if (handler != null) {
526538
handler.post(new Runnable() {
527539
@Override
528540
public void run() {
529-
getUploadListener().onUpload(BaseHttpRequest.this, (float) current / total, current, total, done);
541+
getUploadListener().onUpload(BaseHttpRequest.this, (float) current / total, current, total, done, error);
530542
}
531543
});
532544
} else {
533-
getUploadListener().onUpload(BaseHttpRequest.this, (float) current / total, current, total, done);
545+
getUploadListener().onUpload(BaseHttpRequest.this, (float) current / total, current, total, done, error);
534546
}
535547
}
548+
if (error == null) {
549+
updateProgressCurrent = current;
550+
updateProgressTotal = total;
551+
updateProgressDone = done;
552+
}
536553
}
537554

538-
private void callDownloadingCallback(float progress, long sum, long total) {
555+
float downloadProgress;
556+
long downloadProgressCurrent;
557+
long downloadProgressTotal;
558+
559+
private void callDownloadingCallback(float progress, long current, long total, Exception error) {
539560
if (getDownloadListener() == null) return;
540-
LockLog.logI(TAG_RETURN, "下载:" + getUrl() + " 进度:" + progress + " 已下载:" + sum + " 总共:" + total);
561+
LockLog.logI(TAG_RETURN, "下载:" + getUrl() + " 进度:" + progress + " 已下载:" + current + " 总共:" + total);
541562
if (callbackInMainLooper) {
542563
Looper mainLooper = Looper.getMainLooper();
543564
handler = new Handler(mainLooper);
544565
handler.post(new Runnable() {
545566
@Override
546567
public void run() {
547-
getDownloadListener().onDownload(BaseHttpRequest.this, downloadFile, progress, sum, total, progress >= 1f);
568+
getDownloadListener().onDownload(BaseHttpRequest.this, downloadFile, progress, current, total, progress >= 1f, error);
548569
}
549570
});
550571
} else {
551572
if (handler != null) {
552573
handler.post(new Runnable() {
553574
@Override
554575
public void run() {
555-
getDownloadListener().onDownload(BaseHttpRequest.this, downloadFile, progress, sum, total, progress >= 1f);
576+
getDownloadListener().onDownload(BaseHttpRequest.this, downloadFile, progress, current, total, progress >= 1f, error);
556577
}
557578
});
558579
} else {
559-
getDownloadListener().onDownload(BaseHttpRequest.this, downloadFile, progress, sum, total, progress >= 1f);
580+
getDownloadListener().onDownload(BaseHttpRequest.this, downloadFile, progress, current, total, progress >= 1f, error);
560581
}
561582
}
583+
if (error == null) {
584+
downloadProgress = progress;
585+
downloadProgressCurrent = current;
586+
downloadProgressTotal = total;
587+
}
562588
}
563589

564590
private @Nullable RequestBody createOriginRequestBody() {
@@ -978,15 +1004,17 @@ public boolean isRequesting() {
9781004
public void setRequesting(boolean requesting) {
9791005
this.requesting = requesting;
9801006
if (requesting) {
981-
timeoutChecker = new Timer();
982-
timeoutChecker.schedule(new TimerTask() {
983-
@Override
984-
public void run() {
985-
if (isRequesting()) {
986-
onFail(new TimeOutException());
1007+
if (!streamRequest) {
1008+
timeoutChecker = new Timer();
1009+
timeoutChecker.schedule(new TimerTask() {
1010+
@Override
1011+
public void run() {
1012+
if (isRequesting()) {
1013+
onFail(new TimeOutException());
1014+
}
9871015
}
988-
}
989-
}, timeoutDuration * 1000);
1016+
}, timeoutDuration * 1000);
1017+
}
9901018
setLifecycleState(Lifecycle.State.STARTED);
9911019
} else {
9921020
if (timeoutChecker != null) {
@@ -1506,4 +1534,58 @@ public Exception getResponseException() {
15061534
public boolean isError() {
15071535
return responseException == null;
15081536
}
1537+
1538+
/**
1539+
* 获取上传进度的已上传字节数
1540+
*
1541+
* @return 已上传字节数
1542+
*/
1543+
public long getUpdateProgressCurrent() {
1544+
return updateProgressCurrent;
1545+
}
1546+
1547+
/**
1548+
* 获取上传进度的总字节数
1549+
*
1550+
* @return 总字节数
1551+
*/
1552+
public long getUpdateProgressTotal() {
1553+
return updateProgressTotal;
1554+
}
1555+
1556+
/**
1557+
* 获取上传进度是否完成
1558+
*
1559+
* @return 是否完成
1560+
*/
1561+
public boolean isUpdateProgressDone() {
1562+
return updateProgressDone;
1563+
}
1564+
1565+
/**
1566+
* 获取下载进度的百分比
1567+
*
1568+
* @return 当前下载进度(0~1)
1569+
*/
1570+
public float getDownloadProgress() {
1571+
return downloadProgress;
1572+
}
1573+
1574+
/**
1575+
* 获取下载进度的已完成字节数
1576+
*
1577+
* @return 已完成字节数
1578+
*/
1579+
public long getDownloadProgressCurrent() {
1580+
return downloadProgressCurrent;
1581+
}
1582+
1583+
/**
1584+
* 获取下载进度的总字节数
1585+
*
1586+
* @return 总字节数
1587+
*/
1588+
public long getDownloadProgressTotal() {
1589+
return downloadProgressTotal;
1590+
}
15091591
}

README.md

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
</div>
55

66

7+
78
基于 OkHttp 的二次封装网络请求框架,更简洁易用,符合应用开发者使用习惯,能够自动处理异步线程问题,具备丰富的扩展性,全局拦截器以及实用的日志输出控制,让网络请求变得更加简单。
89

910
<div align=center>
@@ -22,6 +23,7 @@
2223
</div>
2324

2425

26+
2527
## BaseOkHttpX 优势
2628

2729
- **全新升级:** BaseOkHttpX 是 [BaseOkHttpV3](https://github.com/kongzue/BaseOkHttpV3) 的升级重构版本,重做了因旧版代码不断迭代导致的冗余问题,代码结构更为清晰明了
@@ -189,6 +191,7 @@ SentencesBean bean = jsonMap.toBean(SentencesBean.class);
189191
//要将 Bean 反向转为 JsonMap,可以使用:
190192
JsonMap jsonData = JsonMap.toJsonMap(bean);
191193
```
194+
192195
详情请参阅 [BaseJson](https://github.com/kongzue/BaseJson) 的说明文档
193196

194197
### 添加参数
@@ -333,8 +336,10 @@ Post.create(MainActivity.this, "/api/login")
333336
```java
334337
.setUploadListener(new UploadListener() {
335338
@Override
336-
public void onUpload(BaseHttpRequest httpRequest, float progress, long current, long total, boolean done) {
337-
339+
public void onUpload(BaseHttpRequest httpRequest, float progress, long current, long total, boolean done, Exception error) {
340+
if (error == null) {
341+
//...
342+
}
338343
}
339344
})
340345
```
@@ -349,7 +354,10 @@ Post.create(MainActivity.this, "/api/login")
349354
getRequest(MainActivity.this, "https://dl.coolapk.com/down?pn=com.coolapk.market&id=NDU5OQ&h=46bb9d98&from=from-web")
350355
.downloadToFile(cacheFile, new DownloadListener() {
351356
@Override
352-
public void onDownload(BaseHttpRequest httpRequest, File downloadFile, float progress, long current, long total, boolean done) {
357+
public void onDownload(BaseHttpRequest httpRequest, File downloadFile, float progress, long current, long total, boolean done, Exception error) {
358+
if (error == null) {
359+
//...
360+
}
353361
}
354362
})
355363
.go();
@@ -522,6 +530,24 @@ Get、Post、Delete、Patch、Put 在创建请求后会返回实例化的 `BaseH
522530

523531
// 重新请求
524532
.retry()
533+
534+
// 获取上传进度的已上传字节数
535+
.getUpdateProgressCurrent()
536+
537+
// 获取上传进度的总字节数
538+
.getUpdateProgressTotal()
539+
540+
// 获取上传进度是否完成
541+
.isUpdateProgressDone()
542+
543+
// 获取下载进度的百分比
544+
.getDownloadProgress()
545+
546+
// 获取下载进度的已完成字节数
547+
.getDownloadProgressCurrent()
548+
549+
//获取下载进度的总字节数
550+
.getDownloadProgressTotal()
525551
```
526552

527553
## ToDo
@@ -555,4 +581,4 @@ distributed under the License is distributed on an "AS IS" BASIS,
555581
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
556582
See the License for the specific language governing permissions and
557583
limitations under the License.
558-
```
584+
```

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ android {
99
minSdk 21
1010
targetSdk 31
1111
versionCode 7
12-
versionName "1.2.5"
12+
versionName "1.2.6"
1313

1414
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1515
}

app/src/main/java/com/kongzue/baseokhttpx/demo/MainActivity.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,18 @@ public void onClick(View v) {
117117
.downloadToFile(cacheFile, new DownloadListener() {
118118

119119
@Override
120-
public void onDownload(BaseHttpRequest httpRequest, File downloadFile, float progress, long current, long total, boolean done) {
121-
binding.imgResult.setVisibility(View.GONE);
122-
binding.txtResult.setVisibility(View.VISIBLE);
123-
String progressText = (done ? "下载完毕:" : "正在下载:") + " 进度:" + new BigDecimal(progress).setScale(4, RoundingMode.HALF_UP).toPlainString() + " 总共:" + total + " 已完成:" + current;
124-
if (binding.txtResult.getText().length() == 0) {
125-
binding.txtResult.setText(progressText);
120+
public void onDownload(BaseHttpRequest httpRequest, File downloadFile, float progress, long current, long total, boolean done, Exception error) {
121+
if (error == null) {
122+
binding.imgResult.setVisibility(View.GONE);
123+
binding.txtResult.setVisibility(View.VISIBLE);
124+
String progressText = (done ? "下载完毕:" : "正在下载:") + " 进度:" + new BigDecimal(progress).setScale(4, RoundingMode.HALF_UP).toPlainString() + " 总共:" + total + " 已完成:" + current;
125+
if (binding.txtResult.getText().length() == 0) {
126+
binding.txtResult.setText(progressText);
127+
} else {
128+
binding.txtResult.append("\n" + progressText);
129+
}
126130
} else {
127-
binding.txtResult.append("\n" + progressText);
131+
binding.txtResult.setText("下载失败:" + error);
128132
}
129133
}
130134
})

0 commit comments

Comments
 (0)