2013-05-14 64 views
2

後的研究2天后,我沒有發現任何解決我的問題:/安卓:從URL下載內容返回MP3文件

我想下載一個MP3文件,該文件被調用後纔會返回瀏覽器中的URL。

我不能給你實際的URL,因爲我不允許由於版權限制,但它的形式是:

http://wscompany.name.com/downloadws/getDlFile/mdkHdKy97RppVWOsIOdDBuG/audio/1478 

正如你可以看到,這個URL沒有mp3擴展。所以如果我在窗口上的瀏覽器中放入這種URL,它會返回一個MP3以保存到磁盤上,這很好。但如果我想在android中調用此URL來下載最終返回的文件(mp3),它不起作用。

我試着用包含一個mp3文件的url直接和它工作得很好(如http://www.mediacollege.com/downloads/sound-effects/urban/factory/Factory_External_01.mp3),但沒有沒有mp3擴展名的網址,即使它確實返回一個mp3,我希望你明白我的意思。

有誰知道如何在android中做到這一點?

這裏是我的代碼,使用的AsyncTask,通過

new Download(MyActivity.this, urlToCall).execute(); 

,下載的AsyncTask叫:

public class Download extends AsyncTask<String, Void, String> 
{ 
    ProgressDialog mProgressDialog; 

    Context context; 
    String urlDownload; 

    public Download(Context context,String url) 
    { 
     this.context = context; 
     this.urlDownload=url; 
    } 

    protected void onPreExecute() 
    { 
     mProgressDialog = ProgressDialog.show(context, "","Please wait, Download for " + urlDownload); 
     Log.v("DOWNLOAD", "Wait for downloading url : " + urlDownload); 
    } 

    protected String doInBackground(String... params) 
    { 
     try 
     { 
      //URL url = new URL("http://www.mediacollege.com/downloads/sound-effects/urban/factory/Factory_External_01.mp3"); 
      URL url = new URL(urlDownload); 

      Log.w("DOWNLOAD" , "URL TO CALL : " + url.toString()); 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

      //set up some things on the connection 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.setDoOutput(true); 
      urlConnection.connect(); 

      File folder = new File(Environment.getExternalStorageDirectory()+ "/MyDownloaded/") ; 

      boolean success = true; 
      if (!folder.exists()) { 
       success = folder.mkdir(); 
      } 

      File file = new File(folder,"somefile.mp3"); 

      FileOutputStream fileOutput = new FileOutputStream(file); 
      InputStream inputStream = urlConnection.getInputStream(); 

      //this is the total size of the file 
      int totalSize = urlConnection.getContentLength(); 
      //variable to store total downloaded bytes 
      int downloadedSize = 0; 

      //create a buffer... 
      byte[] buffer = new byte[1024]; 
      int bufferLength = 0; //used to store a temporary size of the buffer 

      //now, read through the input buffer and write the contents to the file 
      while ((bufferLength = inputStream.read(buffer)) > 0) { 
       //add the data in the buffer to the file in the file output stream (the file on the sd card 
       fileOutput.write(buffer, 0, bufferLength); 
       //add up the size so we know how much is downloaded 
       downloadedSize += bufferLength; 
       //this is where you would do something to report the prgress, like this maybe 
       //updateProgress(downloadedSize, totalSize); 
       Log.w("DOWNLOAD" , "progress " + downloadedSize + "/" + totalSize); 

      } 
      //close the output stream when done 
      fileOutput.close(); 

     //catch some possible errors... 
     } 
     catch (MalformedURLException e) 
     { 
      Log.e("DOWNLOAD" , "ERROR : " + e); 
     } 
     catch (IOException e) 
     { 
      Log.e("DOWNLOAD" , "ERROR : " + e); 
     } 
     return "done"; 
    } 

    private void publishProgress(int i) 
    { 
     Log.v("DOWNLOAD", "PROGRESS ... " + i); 
    } 

    protected void onPostExecute(String result) 
    { 
     if (result.equals("done")) 
      mProgressDialog.dismiss(); 
    } 

謝謝你在前進,我希望有人能幫助我:)

DevJ

+1

請在評論中寫下downvoting的原因。像雞一樣降低和隱藏是不公平的。 – 2013-05-14 07:41:49

+0

我同意,我沒有看到這個問題沒有錯(看起來英語不是OP的原始語言,但我們應該始終努力嘗試理解,並始終留下評論爲什麼) – 2013-05-14 07:45:26

+0

對不起,我的英語差..我做了很多努力說好.. – DevJ 2013-05-14 07:49:32

回答

0

您可以在桌面PC上使用網絡嗅探器(如Wireshark)看看請求MP3時到底發生了什麼。我的猜測是,有a redirect到另一個URL。

1

很可能初始URL包含狀態302重定向到實際承載或提供mp3的ANOTHER頁面。有一個名爲Jsoup的Java庫,您可以使用它來獲取實際的mp3文件並下載它。因此,讓我們說你有初始網址: http://wscompany.name.com/downloadws/getDlFile/mdkHdKy97RppVWOsIOdDBuG/audio/1478

你應該做的就是打開你的瀏覽器的第一件事,右鍵點擊在屏幕任意位置,並選擇「檢查元素」或類似的東西,這樣就可以監控網絡交通。在檢查元素窗格打開的情況下,輸入上面的URL,您應該看到它首先進入該URL(顯示302重定向狀態),然後轉到另一個URL(可能),然後結束目標(具有狀態200)。狀態200頁面是您想要的實際頁面。您可以像這樣在Jsoup中輸入以上URL:

Document doc = Jsoup.connect(「http://wscompany.name.com/downloadws/getDlFile/mdkHdKy97RppVWOsIOdDBuG/audio/1478」).followredirect(true).header(「」,「」)。header(「」,「」)。 ... etc .post()或.get();

它返回一個Document對象(閱讀Jsoup文檔)。這個Document對象可以被解析以獲得某些數據......就像您可以傳遞給Jsoup.connect方法的其他URL一樣,直到您找到您想要用Android下載的實際mp3。

查看「inspect元素」窗格中列出的請求標頭並將這些標頭通過多個.header()方法鏈接到.connect()方法將很重要。

我知道這個回答太晚了2年,但希望對於任何仍然需要做你正在嘗試的人都會有所幫助。