Android DownloadManager failed downloading with reason ERROR_TOO_MANY_REDIRECTS, how can I fix this issue for targeting API level 31 or higher?

huangapple go评论80阅读模式
英文:

Android DownloadManager failed downloading with reason ERROR_TOO_MANY_REDIRECTS, how can I fix this issue for targeting API level 31 or higher?

问题

使用android.app.DownloadManager下载文件,但有些文件下载失败,原因是ERROR_TOO_MANY_REDIRECTS。

我从下面的代码中获取了失败的原因:

  1. val reasonIndex = cursor.getColumnIndex(DownloadManager.COLUMN_REASON)
  2. val reason = cursor.getInt(reasonIndex)
  3. // public final static int ERROR_TOO_MANY_REDIRECTS = 1005;
  4. // 我得到了相同的原因1005

是否有办法增加maxRedirect以及默认的重定向次数是多少?

如何解决这个过多重定向的问题?

我目前使用类似以下的代码:

  1. val downloadManager: DownloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
  2. fun downloadFile(url: String, path: String, title: String? = null): Long {
  3. val downloadUri = Uri.parse(url)
  4. var downloadId: Long
  5. try {
  6. val request = DownloadManager.Request(downloadUri).apply {
  7. setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
  8. .setAllowedOverRoaming(false)
  9. .setTitle(title ?: "")
  10. .setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN)
  11. .setDestinationUri(Uri.fromFile(File(path)))
  12. }
  13. downloadId = downloadManager?.enqueue(request) ?: -1
  14. scanMedia(path)
  15. } catch (exception: Exception) {
  16. downloadId = -1
  17. }
  18. return downloadId
  19. }

我想要从android.app.DownloadManager下载文件,即使URL重定向了10次、20次或30次。

英文:

I am using android.app.DownloadManager for downloading file, but few files failed download, with reason ERROR_TOO_MANY_REDIRECTS,

I got the reason to failed from below code:

  1. val reasonIndex = cursor.getColumnIndex(DownloadManager.COLUMN_REASON)
  2. val reason = cursor.getInt(reasonIndex)
  3. //public final static int ERROR_TOO_MANY_REDIRECTS = 1005;
  4. // I got same reason 1005

Is there any way to increase maxRedirect and what is default redirect count?

How can I fixed this too many redirect issue?

Currently I am using similar to below code:

  1. val downloadManager: DownloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
  2. fun downloadFile(url: String, path: String, title: String? = null): Long {
  3. val downloadUri = Uri.parse(url)
  4. var downloadId: Long
  5. try {
  6. val request = DownloadManager.Request(downloadUri).apply {
  7. setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
  8. .setAllowedOverRoaming(false)
  9. .setTitle(title ?: "")
  10. .setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN)
  11. .setDestinationUri(Uri.fromFile(File(path)))
  12. }
  13. downloadId = downloadManager?.enqueue(request) ?: -1
  14. scanMedia(path)
  15. } catch (exception: Exception) {
  16. downloadId = -1
  17. }
  18. return downloadId
  19. }

I want to download file from android.app.DownloadManager even if the url redirect to more then 10, 20 or 30 times

答案1

得分: 0

为了解决这个问题,我们首先需要获取重定向的URL,并将最终的重定向URL提供给下载管理器进行下载。这是解决这个问题的方法。

  1. @Throws(IOException::class)
  2. suspend fun getFinalURL1(url: String, totalTimeOut: Long): String {
  3. return withContext(Dispatchers.IO) {
  4. if (totalTimeOut < System.currentTimeMillis()) {
  5. return@withContext url
  6. }
  7. delay(10)
  8. val obj = URL(url)
  9. val con: HttpURLConnection = obj.openConnection() as HttpURLConnection
  10. con.instanceFollowRedirects = false
  11. con.connect()
  12. con.inputStream
  13. if (con.responseCode == HttpURLConnection.HTTP_MOVED_PERM || con.responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
  14. var redirectUrl: String? = con.getHeaderField("Location")
  15. if (redirectUrl == null) {
  16. return@withContext url
  17. } else if (redirectUrl.startsWith("/")) {
  18. redirectUrl = obj.protocol.toString() + "://" + obj.host + redirectUrl
  19. }
  20. if (!redirectUrl.startsWith(startsWith, true)) {
  21. return@withContext url
  22. }
  23. return@withContext getFinalURL1(redirectUrl, totalTimeOut)
  24. }
  25. return@withContext url
  26. }
  27. }
英文:

To solve this issue, we have to first get the redirected url and give the final redirected url to donwnload manager to download.
This is the way to solve this issue.

  1. @Throws(IOException::class)
  2. suspend fun getFinalURL1(url: String, totalTimeOut: Long): String {
  3. return withContext(Dispatchers.IO) {
  4. if (totalTimeOut &lt; System.currentTimeMillis()) {
  5. return@withContext url
  6. }
  7. delay(10)
  8. val obj = URL(url)
  9. val con: HttpURLConnection = obj.openConnection() as HttpURLConnection
  10. con.instanceFollowRedirects = false
  11. con.connect()
  12. con.inputStream
  13. if (con.responseCode == HttpURLConnection.HTTP_MOVED_PERM || con.responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
  14. var redirectUrl: String? = con.getHeaderField(&quot;Location&quot;)
  15. if (redirectUrl == null) {
  16. return@withContext url
  17. } else if (redirectUrl.startsWith(&quot;/&quot;)) {
  18. redirectUrl = obj.protocol.toString() + &quot;://&quot; + obj.host + redirectUrl
  19. }
  20. if (!redirectUrl.startsWith(startsWith, true)) {
  21. return@withContext url
  22. }
  23. return@withContext getFinalURL1(redirectUrl, totalTimeOut)
  24. }
  25. return@withContext url
  26. }
  27. }

huangapple
  • 本文由 发表于 2023年7月10日 15:47:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651695-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定