美文网首页
Android 使用DownloadManager下载apk失败

Android 使用DownloadManager下载apk失败

作者: Kndroid | 来源:发表于2018-04-23 15:10 被阅读0次

一、问题

使用DownloadManager下载App失败,以往都是好好的,代码也没有动过,怎么就下载失败了呢?

后端从http更换为https

对DownloadManager状态的记录:

打印日志

结果:

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;

未知的错误状态,坑了~

二、解决

①怀疑是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,放弃。

②既然两种方式都支持,而且多个版本迭代以来都未发生过这样的问题,所以一直就抱着怀疑后端存在处理的问题。

果然后端下发的url未http的,当我们访问http的链接时,后端做了一个重定向,既重新转成https的方式,导致DownloadManager无法正常下载了。

三、最后

在使用DownloadManager做下载时需要注意,直接给物理地址,不要有任何的重定向,否则无法正常下载。

相关文章

网友评论

      本文标题:Android 使用DownloadManager下载apk失败

      本文链接:https://www.haomeiwen.com/subject/vxhllftx.html