@@ -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}
0 commit comments