经过两天的研究,我没有找到任何解决我的问题的方法:
我想下载一个mp3文件,该文件只有在浏览器中调用URL后才会返回。
我不能给你实际的url,因为由于权限限制,我不能给你,但它的形式是:
http://wscompany.name.com/downloadws/getDlFile/mdkHdKy97RppVWOsIOdDBuG/audio/1478
如您所见,此URL没有mp3扩展名。因此,如果我将这种URL放在windows的浏览器中,它会返回一个MP3保存到磁盘上,这是可以的。但是如果我想在android中调用这个URL来下载最终返回的文件( mp3),它就不能工作。
我尝试使用直接包含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();
和下载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
发布于 2015-09-04 10:45:01
初始URL很可能包含状态302,重定向到实际托管或提供mp3的另一个页面。有一个名为Jsoup的Java库,您可以使用它来获取实际的mp3文件并下载它。因此,假设您的初始网址为:http://wscompany.name.com/downloadws/getDlFile/mdkHdKy97RppVWOsIOdDBuG/audio/1478
你应该做的第一件事是打开你的浏览器,右键点击屏幕上的任何地方,然后选择"inspect element“或类似的东西,这样你就可以监控网络流量了。在inspect element窗格打开的情况下,输入上面的URL,您应该看到它首先转到该URL (显示状态为302 redirect),然后转到另一个URL (可能),最后到达目的地(状态为200)。status 200页面是您想要的实际页面。您可以在Jsoup中输入上述URL,如下所示:
文档文档= Jsoup.connect("http://wscompany.name.com/downloadws/getDlFile/mdkHdKy97RppVWOsIOdDBuG/audio/1478").followredirect(true).header("","").header("",“”)。...etc .post()或.get();
它返回一个Document对象(请阅读Jsoup文档)。这个Document对象可以被解析以获得某些data...like其他URL,您也可以将这些URL传递给Jsoup.connect方法,直到您获得想要用安卓下载的实际URL。
重要的是要查看"inspect element“窗格中列出的请求头,并通过使用多个.header()方法将它们链接起来,将这些头添加到.connect()方法中。
我知道这个回复太晚了两年,但希望它能对任何需要做你正在尝试的事情的人有所帮助。
发布于 2013-05-14 15:58:56
您可以在台式PC上使用网络嗅探器(如Wireshark)来查看在请求MP3时到底发生了什么。我的猜测是有指向另一个网址的a redirect。
https://stackoverflow.com/questions/16537568
复制相似问题