Android : download content from URL which returns mp3 file -


after 2 days of research, didn't found solution problem :/

i want download mp3 file, returned after calling url in browser.

i can't give actual url because i'm not allowed due rights restrictions, of form:

http://wscompany.name.com/downloadws/getdlfile/mdkhdky97rppvwosioddbug/audio/1478 

as can see, url doesn't have mp3 extension. if put kind of url in browser on windows, returns mp3 save onto disk, fine. if want call url in android in order download final file returned (the mp3), doesn't work.

i tried url containing mp3 file directly , works (like http://www.mediacollege.com/downloads/sound-effects/urban/factory/factory_external_01.mp3), not url without mp3 extension, though returns mp3, hope see mean.

does know how in android ?

here code, using asynctask, called by

new download(myactivity.this, urltocall).execute(); 

and 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 " + urldownload );         log.v("download", "wait 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 call : " + url.tostring());             httpurlconnection urlconnection = (httpurlconnection) url.openconnection();              //set things on 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 total size of file             int totalsize = urlconnection.getcontentlength();             //variable store total downloaded bytes             int downloadedsize = 0;              //create buffer...             byte[] buffer = new byte[1024];             int bufferlength = 0; //used store temporary size of buffer              //now, read through input buffer , write contents file             while ( (bufferlength = inputstream.read(buffer)) > 0 ) {                 //add data in buffer file in file output stream (the file on sd card                 fileoutput.write(buffer, 0, bufferlength);                 //add size know how downloaded                 downloadedsize += bufferlength;                 //this report prgress, maybe                 //updateprogress(downloadedsize, totalsize);                 log.w( "download" , "progress " + downloadedsize + " / " + totalsize);              }             //close output stream when done             fileoutput.close();          //catch possible errors...         }          catch (malformedurlexception e)          {             log.e( "download" , "error : " + e );         }          catch (ioexception e)          {             log.e( "download" , "error : " + e );         }         return "done";     }      private void publishprogress( int )     {         log.v("download", "progress ... " + i);     }      protected void onpostexecute(string result)      {         if (result.equals("done"))              mprogressdialog.dismiss();     } 

thank in advance, hope me :)

devj

more initial url contains status 302 redirect page hosts or provides mp3. there java library called jsoup can use actual mp3 file , download it. let's have initial url of: http://wscompany.name.com/downloadws/getdlfile/mdkhdky97rppvwosioddbug/audio/1478

the first thing should open browser, right click anywhere on screen , choose "inspect element" or similar can monitor network traffic. inspect element pane open, enter above url , should see first goes url (shows status of 302 redirect), goes url (possibly), before ending @ destination (with status 200). status 200 page actual page want. can enter above url in jsoup this:

document doc = jsoup.connect("http://wscompany.name.com/downloadws/getdlfile/mdkhdky97rppvwosioddbug/audio/1478").followredirect(true).header("","").header("",""). ...etc .post() or .get();

it returns document object (read jsoup documentation). document object can parsed data...like other urls can pass jsoup.connect method, until actual mp3 want download android.

it important @ request headers listed in "inspect element" pane , add headers .connect() method chaining them multiple .header() methods.

i know response 2 years late, helpful still needing attempting.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -