2

2日間の調査の後、問題の解決策が見つかりませんでした:/

ブラウザーで URL を呼び出した後にのみ返される mp3 ファイルをダウンロードしたいと考えています。

権利の制限により許可されていないため、実際の URL を提供することはできませんが、形式は次のとおりです。

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

ご覧のとおり、この URL には mp3 拡張子がありません。したがって、この種の URL を Windows のブラウザに入力すると、MP3 が返されてディスクに保存されますが、これは問題ありません。しかし、返された最終ファイル (mp3) をダウンロードするために Android でこの URL を呼び出したい場合、機能しません。

mp3ファイルを直接含むURLを試してみましたが、非常にうまく機能します( http://www.mediacollege.com/downloads/sound-effects/urban/factory/Factory_External_01.mp3のように)が、mp3のないURLでは機能しませんmp3 を返しますが、私の言いたいことがわかると思います。

Androidでそれを行う方法を知っている人はいますか?

によって呼び出された AsyncTask を使用した私のコードは次のとおりです。

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

そして Download 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

4

2 に答える 2

1

おそらく最初の URL には、実際に mp3 をホストまたは提供する別のページへのステータス 302 リダイレクトが含まれています。実際の mp3 ファイルを取得してダウンロードするために使用できる Jsoup という Java ライブラリがあります。したがって、最初の URL が http://wscompany.name.com/downloadws/getDlFile/mdkHdKy97RppVWOsIOdDBuG/audio/1478であるとします。

最初に行うべきことは、ブラウザを開き、画面上の任意の場所を右クリックして、「要素の検査」などを選択して、ネットワーク トラフィックを監視できるようにすることです。要素の検査ペインを開いた状態で、上記の URL を入力すると、最初にその URL に移動し (302 リダイレクトのステータスを表示)、次に別の URL (おそらく) に移動してから、宛先に到達します (ステータスは200)。ステータス 200 ページは、必要な実際のページです。上記の URL を次のように Jsoup に入力できます。

ドキュメント doc = Jsoup.connect(" http://wscompany.name.com/downloadws/getDlFile/mdkHdKy97RppVWOsIOdDBuG/audio/1478 ").followredirect(true).header("","").header(""," 」)。...etc .post() または .get();

Document オブジェクトを返します (Jsoup のドキュメントを参照してください)。この Document オブジェクトを解析して特定のデータを取得できます... Android でダウンロードする実際の mp3 に到達するまで、Jsoup.connect メソッドに渡すこともできるその他の URL などです。

「要素の検査」ペインにリストされている要求ヘッダーを確認し、それらのヘッダーを複数の .header() メソッドでチェーンして .connect() メソッドに追加することが重要です。

この回答が 2 年遅すぎることは承知していますが、あなたが試みていたことをまだ実行する必要がある人の役に立てば幸いです。

于 2015-09-04T02:45:01.210 に答える