Android xutils analyzes and modifies the download progress update frequency

Source: Internet
Author: User

Android xutils analyzes and modifies the download progress update frequency


Because there is a point card during the update progress, we want to slow down the interval of the update progress. Make it less frequent.

Directly look at the code analysis.

HttpHandler. java implements the download progress listener of RequestCallBackHandler.


Private ResponseInfo
 
  
HandleResponse (HttpResponse response) throws HttpException, IOException {if (response = null) {throw new HttpException ("response is null");} if (isCancelled () return null; statusLine status = response. getStatusLine (); int statusCode = status. getStatusCode (); if (statusCode <300) {Object result = null; HttpEntity entity = response. getEntity (); if (entity! = Null) {isUploading = false; if (isDownloadingFile) {autoResume = autoResume & OtherUtils. isSupportRange (response); String responseFileName = autoRename? OtherUtils. getFileNameFromHttpResponse (response): null; FileDownloadHandler downloadHandler = new FileDownloadHandler (); result = downloadHandler. handleEntity (entity, this, fileSavePath, autoResume, responseFileName); // here I have found the entity implemented by the progress interface. Let's click to see it.} Else {StringDownloadHandler downloadHandler = new StringDownloadHandler (); result = downloadHandler. handleEntity (entity, this, charset); if (HttpUtils. sHttpCache. isEnabled (requestMethod) {HttpUtils. sHttpCache. put (requestUrl, (String) result, expiry) ;}} return new ResponseInfo
  
   
(Response, (T) result, false);} else if (statusCode = 301 | statusCode = 302) {if (httpRedirectHandler = null) {httpRedirectHandler = new DefaultHttpRedirectHandler ();} HttpRequestBase request = httpRedirectHandler. getDirectRequest (response); if (request! = Null) {return this. sendRequest (request) ;}} else if (statusCode = 416) {throw new HttpException (statusCode, "maybe the file has downloaded completely ");} else {throw new HttpException (statusCode, status. getReasonPhrase ();} return null ;}
  
 

In FileDownloadHandler. java, xutils performs the following operations on the Progress Update.

If you are in the Progress Update Status, you can set the update progress at intervals. If you are in another status, no interval is set.

Public class FileDownloadHandler {public File handleEntity (HttpEntity entity, RequestCallBackHandler callBackHandler, String target, boolean isResume, String responseFileName) throws IOException {if (entity = null | TextUtils. isEmpty (target) {return null;} File targetFile = new File (target); if (! TargetFile. exists () {File dir = targetFile. getParentFile (); if (dir. exists () | dir. mkdirs () {targetFile. createNewFile () ;}} long current = 0; BufferedInputStream bis = null; BufferedOutputStream bos = null; try {FileOutputStream fileOutputStream = null; if (isResume) {current = targetFile. length (); fileOutputStream = new FileOutputStream (target, true);} else {fileOutputStream = new FileOu TputStream (target);} long total = entity. getContentLength () + current; bis = new BufferedInputStream (entity. getContent (); bos = new BufferedOutputStream (fileOutputStream); if (callBackHandler! = Null &&! CallBackHandler. updateProgress (total, current, true) {return targetFile;} byte [] tmp = new byte [4096]; int len; while (len = bis. read (tmp ))! =-1) {bos. write (tmp, 0, len); current + = len; if (callBackHandler! = Null) {if (! CallBackHandler. updateProgress (total, current, false) {// Progress Update, with update frequency: return targetFile ;}} bos. flush (); if (callBackHandler! = Null) {callBackHandler. updateProgress (total, current, true); // must be updated} finally {IOUtils. closeQuietly (bis); IOUtils. closeQuietly (bos);} if (targetFile. exists ()&&! TextUtils. isEmpty (responseFileName) {File newFile = new File (targetFile. getParent (), responseFileName); while (newFile. exists () {newFile = new File (targetFile. getParent (), System. currentTimeMillis () + responseFileName);} return targetFile. renameTo (newFile )? NewFile: targetFile;} else {return targetFile ;}}}



Finally, let's go back to HttpHandler. java.

Private long lastUpdateTime; @ Override public boolean updateProgress (long total, long current, boolean forceUpdateUI) {if (callback! = Null & this. state! = State. CANCELLED) {if (forceUpdateUI) {this. publishProgress (UPDATE_LOADING, total, current);} else {long currTime = SystemClock. uptimeMillis (); if (currTime-lastUpdateTime> = callback. getRate () {lastUpdateTime = currTime; this. publishProgress (UPDATE_LOADING, total, current); // Update Progress} return this. state! = State. CANCELLED ;}


Finally, where can I set it. The interval.

To RequestCallBack. java



Private static final int DEFAULT_RATE = 1000;


Private static final int MIN_RATE = 200;

// Modify it here. Finished

Public final int getRate (){
If (rate <MIN_RATE ){
Return MIN_RATE;
}
Return rate;
}

You can also set rate (2000) in callback.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.