I'm trying to get a zip file using DownloadManager parsing a cookie with a JSESSIONID from my server. I'm getting this JSESSIONID doing all my process login using HTTPCLIENT lib and setting a variable JSESSIONID for later use in my DownloadManager request.
My download request:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(htmlUrlDownload));
request.addRequestHeader("Cookie", "JSESSIONID=" + JSESSIONID);
request.addRequestHeader(Constants.USER_AGENT, Constants.TARGET_REQUEST_HEADER);
request.setDescription("Baixando " + metaDado.getType());
request.setTitle("Download");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
String nameFile = offlineUuid + ".zip";
fileName = nameFile;
filePath = Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_DOWNLOADS
+ File.separator + fileName;
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, nameFile);
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
The problem is that download never starts and the COLUMN_REASON returns a code to ERROR_UNHANDLED_HTTP_CODE
But if I try to download the same file using a dropbox link without authentication or using httpclient, it's works perfectly, what am I doing wrong?
How can I get a better msg error?
Nothing wrong in your code, seems like your server is responding with HTTP redirect staus code, which is causing DownloadManager to fail to process download.
From Android Docs :-
public static final int ERROR_UNHANDLED_HTTP_CODE
Added in API level 9 Value of COLUMN_REASON when an HTTP code was received that download manager can't handle.
See below code snippet:-
From Android Framework source code DownloadManger.java
case Downloads.Impl.STATUS_UNHANDLED_HTTP_CODE:
case Downloads.Impl.STATUS_UNHANDLED_REDIRECT:
return ERROR_UNHANDLED_HTTP_CODE;
So you need to check your Server logs for this issue, or pass direct file url to download manager which doesn't cause any redirect.
Also please note that you need below two permissions in your Androidmanifest.xml for download to work as expected
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With