Download from URLConnection : Url « Network « Android






Download from URLConnection

 

//package org.mimp.newimp;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URLConnection;

class NetUtil {
    public static byte[] download(URLConnection cnx) {
        byte[] dat = null;
        try {
            InputStream is = cnx.getInputStream();
            int len = cnx.getContentLength();
            if (len < 0) {
                ByteArrayOutputStream bao = new ByteArrayOutputStream();
                byte[] buf = new byte[4096];
                for (;;) {
                    int nb = is.read(buf);
                    if (nb <= 0)
                        break;
                    bao.write(buf, 0, nb);
                }
                dat = bao.toByteArray();
                bao.close();
            }
            else {
                dat = new byte[len];
                int i = 0;
                while (i < len) {
                    int n = is.read(dat, i, len - i);
                    if (n <= 0)
                        break;
                    i += n;
                }
            }
            is.close();
        }
        catch (Exception ex) {
        }
        return dat;
    }
}

   
  








Related examples in the same category

1.Using Intent to open a URL
2.Process xml document from a Url
3.Suggest Url Provider
4.Showing android:autoLink property, which linkifies things like URLs and phone numbers found in the text.
5.extends ArrayAdapter to create URL history
6.Used to compress URL using the bit.ly service.
7.URL encode and decode
8.Is valid URL
9.Request from an URL
10.Get Url From Img Tag
11.Returns the contents of the given URL as an array of strings
12.Read from a URL
13.Pull the raw text content of the given URL.
14.Get Video from URL
15.Gets data from URL
16.get Url Response
17.URL Encode Utils
18.Downloads a file given URL to specified destination
19.get Host From Url
20.encode Url
21.decode Url
22.Convert a byte array to a URL encoded string
23.get Url content with max retries
24.get Url By Post
25.Take a base url and a {@link Map} of parameters to build a valid url