一、問題
使用DownloadManager下載App失敗,以往都是好好的,代碼也沒有動過,怎么就下載失敗了呢?
后端從http更換為https
對DownloadManager狀態(tài)的記錄:

結果:
E/zlq: bytesDownload:0
E/zlq: descrition:
E/zlq: id:178
E/zlq: localUri:null
E/zlq: mimeType:application/vnd.android.package-archive
E/zlq: title:
E/zlq: status:491
E/zlq: url:https://host/static/app/download/xxx.apk
E/zlq: reason:placeholder
E/zlq: totalSize:-1
最后得到:status:491,既
/**
? ? ? ? * This download has completed with an error.
? ? ? ? * Warning: there will be other status values that indicate errors in
? ? ? ? * the future. Use isStatusError() to capture the entire category.
? ? ? ? */
? ? ? ? public static final int STATUS_UNKNOWN_ERROR = 491;
未知的錯誤狀態(tài),坑了~
二、解決
①懷疑是DownloadManager不支持https
查看源碼:

scheme不是http、https才會拋異常,說明DownloadManager已經可以很好的支持https的下載方式了。
從android4.0開始DownloadManager就已經可以支持https了;看過一篇帖子將DownloadManager改成支持https的方式,不過sdk的版本已經是2.x的了,https://stackoverflow.com/questions/8106155/android-downloadmanager-and-ssl-https,放棄。
②既然兩種方式都支持,而且多個版本迭代以來都未發(fā)生過這樣的問題,所以一直就抱著懷疑后端存在處理的問題。
果然后端下發(fā)的url未http的,當我們訪問http的鏈接時,后端做了一個重定向,既重新轉成https的方式,導致DownloadManager無法正常下載了。
三、最后
在使用DownloadManager做下載時需要注意,直接給物理地址,不要有任何的重定向,否則無法正常下載。