2017-03-23 6 views
1

私のアプリケーションでは、urlconnectionバックグラウンドサービスを使用してファイルをダウンロードしています。この要求には、許可を得るためにサーバーに渡されるトークンが必要です。コードは正常に動作します。ここでは...DownloadManagerリクエストでリクエストプロパティを設定する

  URL url = new URL(fileURL); 

      HttpURLConnection conection = (HttpURLConnection) url.openConnection(); 
      conection.setRequestMethod("GET"); 
      conection.setRequestProperty("User-Agent", "USER_AGENT"); 
      conection.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); 
      conection.setRequestProperty("Authorization", "Token " + token);// evince 
      conection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 
      conection.setRequestProperty("Accept-Encoding", "identity"); 

      int responseCode = conection.getResponseCode(); 

スニペットは今、私はアンドロイドのDownloadManagerを使用するためにダウンロードプロセスを変更したいが、問題は、私はちょうど上記のコードスニペットのようにDownloadManagerのリクエストオブジェクト内のすべてのこれらの要求プロパティを設定しない方法です。これらのプロパティが設定されていなければ、ダウンロードは引き続き失敗するためです。

ありがとうございます。

答えて

1

user-agentと同じ問題がありました。これは私のために働いた:

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); 
0

私はついに研究の後にこれを行う方法を考え出した。

 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); 
関連する問題