经过2天的研究,我没有找到解决问题的方法:/
我想下载一个mp3文件,只有在浏览器中调用URL后才会返回。
我无法向您提供实际网址,因为我因权利限制而不允许这样做,但其格式如下:
http://wscompany.name.com/downloadws/getDlFile/mdkHdKy97RppVWOsIOdDBuG/audio/1478
如您所见,此网址没有mp3扩展名。因此,如果我将这种URL放在Windows上的浏览器中,它会返回一个MP3以保存到磁盘上,这很好。但是如果我想在android中调用这个URL以便下载返回的最终文件(mp3),它就不起作用。
我尝试使用直接包含mp3文件的网址并且效果非常好(例如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
答案 0 :(得分:1)
初始URL很可能包含状态302重定向到实际托管或提供mp3的另一页面。有一个名为Jsoup的Java库,您可以使用它来获取实际的mp3文件并下载它。所以,让我们说你的初始网址是: http://wscompany.name.com/downloadws/getDlFile/mdkHdKy97RppVWOsIOdDBuG/audio/1478
您要做的第一件事就是打开浏览器,右键单击屏幕上的任意位置并选择&#34;检查元素&#34;或类似的东西,以便您可以监控网络流量。打开inspect元素窗格后,输入上面的URL,您应该看到它首先进入该URL(显示302重定向的状态),然后转到另一个URL(可能),然后到达目的地(具有状态) 200)。状态200页面是您想要的实际页面。您可以在Jsoup中输入上述URL,如下所示:
文档doc = Jsoup.connect(&#34; http://wscompany.name.com/downloadws/getDlFile/mdkHdKy97RppVWOsIOdDBuG/audio/1478&#34;)。followredirect(true).header(&#34;&#34;,&#34;&#34;) .header(&#34;&#34;&#34;&#34)。 ......等.post()或.get();
它返回一个Document对象(阅读Jsoup文档)。可以解析此Document对象以获取某些数据...就像您可以传递给Jsoup.connect方法的其他URL一样,直到您获得要使用Android下载的实际mp3。
查看&#34; inspect元素&#34;中列出的请求标头非常重要。窗格并将这些标头添加到.connect()方法中,方法是使用多个.header()方法将它们链接起来。
我知道这个回答已经晚了2年,但希望它对那些仍然需要做你正在尝试的人有帮助。
答案 1 :(得分:0)
您可以在桌面PC上使用网络嗅探器(如Wireshark)来查看请求MP3时的确切情况。我的猜测是a redirect到另一个网址。