在DownloadManager请求中设置Request属性

时间:2017-03-23 11:42:30

标签: android download-manager

我一直在使用var obj = { webSiteName: 'StackOverFlow', find: 'anything', onDays: ['sun' // Object "obj" contains array "onDays" ,'mon', 'tue', 'wed', 'thu', 'fri', 'sat', {name : "jack", age : 34}, // array "onDays"contains array object "manyNames" {manyNames : ["Narayan", "Payal", "Suraj"]}, // ] }; 后台服务来下载我的应用程序中的文件。该请求需要一个令牌,该令牌应传递给服务器以获得许可。代码工作正常。这是一个片段..

urlconnection

现在我想修改下载过程以使用android的DownloadManager,问题是如何在DownloadManager的请求对象中设置所有这些请求属性,就像上面的代码片段一样。因为没有设置这些属性,下载将继续失败。

提前致谢。

2 个答案:

答案 0 :(得分:3)

我终于想到了在研究之后如何做到这一点。

        DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.addRequestHeader("Authorization", "Token " + token);
        request.addRequestHeader("Accept-Language", "en-US,en;q=0.5");
        request.addRequestHeader("Content-Type", "application/json; charset=UTF-8");
        request.addRequestHeader("Accept-Encoding", "identity");
        request.addRequestHeader("User-Agent", "USER_AGENT");

        File direct = new File(Environment.getExternalStorageDirectory() + "/Abeti");
        if (!direct.exists()) {
            direct.mkdirs();
        }

        request.setDestinationInExternalPublicDir("/Abeti", filenamex);
        request.setTitle(cw.getTitle());


        enqueue = downloadManager.enqueue(request);

答案 1 :(得分:2)

与用户代理有同样的问题。这对我有用:

DownloadManager dManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(fileUrl + "?sid=" + sessionId));

request.addRequestHeader("User-Agent", System.getProperty("http.agent") + " myApp_app/" + Utils.appVersionNumber());

dManager.enqueue(request);