ZealDeveloper
ZealDeveloper

Reputation: 783

Android Download Zip From Api And Store in SD CARD

I am working on an API which returns me a zip file containing multiple XML files, which i have to parse individually after extracting the zip file.

Here is the link for that(will download the zip-file) :

http://clinicaltrials.gov/ct2/results?term=&recr=&rslt=&type=&cond=&intr=&outc=&lead=&spons=&id=&state1=&cntry1=&state2=&cntry2=&state3=&cntry3=&locn=&gndr=Female&age=0&rcv_s=&rcv_e=&lup_s=&lup_e=studyxml=true

Here is my current code to save the zip-file in sdcard:

File root = Environment.getExternalStorageDirectory();
String url= "http://clinicaltrials.gov/ct2/results?term=&recr=&rslt=&type=&cond=&intr=&outc=&lead=&spons=&id=&state1=&cntry1=&state2=&cntry2=&state3=&cntry3=&locn=&gndr=Female&age=0&rcv_s=&rcv_e=&lup_s=&lup_e=xml=true";


try {

    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setDoInput(true);
    conn.setConnectTimeout(10000); // timeout 10 secs
    conn.connect();
    InputStream input = conn.getInputStream();

    FileOutputStream fOut = new FileOutputStream(new File(root, "new.zip"));
    int byteCount = 0;
    byte[] buffer = new byte[4096];
    int bytesRead = -1;
    while ((bytesRead = input.read(buffer)) != -1) {

        fOut.write(buffer, 0, bytesRead);
        byteCount += bytesRead;

    }
    fOut.flush();
    fOut.close();

} catch (Exception e) {

    e.printStackTrace();

}

Problem : New.zip File is getting created in sdcard but it seems nothing is downloading also the file size is 0kb. Is my code proper or I have to use something else to handel zipfiles.

Edit Solved :

I am extremely sorry the api link is invalid ... it should be

http://clinicaltrials.gov/ct2/results?term=&recr=&rslt=&type=&cond=&intr=&outc=&lead=&spons=&id=&state1=&cntry1=&state2=&cntry2=&state3=&cntry3=&locn=&gndr=Female&age=0&rcv_s=&rcv_e=&lup_s=&lup_e=&studyxml=true

& is required before studtxml..

Thnx every 1 for quick response ..

Upvotes: 1

Views: 1687

Answers (2)

user370305
user370305

Reputation: 109237

There is something wrong either in your .zip file URL or in .zip file size(0 byte size) because if we download this .zip file (From URL given by you) from web browser then also its downloaded with 0 byte size.

Downloaded .zip file URL.

Upvotes: 1

Related Questions