Android Utililty Methods URL Download

List of utility methods to do URL Download

Description

The list of methods to do URL Download are organized into topic(s).

Method

Stringdownload(String url, String directoryName, String fileName)
Downloads file from specified URL and stores as filename mentioned as parameter in directory name specified use this method when you want to download your imahe into a specific directory and not in default external storage.
Bitmap bmp = getFromUrl(url);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 0, bytes);
File dir = new File(Environment.getExternalStorageDirectory()
        + File.separator + directoryName);
if (!dir.exists()) {
    dir.mkdirs();
File f = new File(Environment.getExternalStorageDirectory()
        + File.separator + directoryName + File.separator
        + fileName);
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
return f.getAbsolutePath();
voiddownloadFile(String downloadUrl, String outputFilePath)
Check file download from url.
ReporterNGExt.logAction("", "",
        String.format("Download file form url: %s", downloadUrl));
CookieStore cookieStore = seleniumCookiesToCookieStore();
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.setCookieStore(cookieStore);
HttpGet httpGet = new HttpGet(downloadUrl);
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
...
voiddownloadFile(URL url, OutputStream output)
download File
InputStream input = null;
HttpURLConnection connection = null;
try {
    connection = (HttpURLConnection) url.openConnection();
    connection.connect();
    if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
        throw new IOException("B??d po??czenia z serwerem: "
                + connection.getResponseCode() + " "
...
StringdownloadHtmlPage(String pageUrl)
download Html Page
try {
    URL url = new URL(pageUrl);
    URLConnection uConn = url.openConnection();
    uConn.setConnectTimeout(15000);
    uConn.setReadTimeout(15000);
    InputStream is = uConn.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);
    ByteArrayBuffer baf = new ByteArrayBuffer(65536);
...
InputStreamdownloadUrl(String urlString)
download Url
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 );
conn.setConnectTimeout(15000 );
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
InputStream stream = conn.getInputStream();
...
StringgetJSONResponseFromURL(String url, Hashtable httpGetParams)
get JSON Response From URL
String json_string = "";
List<NameValuePair> nvps = buildNameValuePair(httpGetParams);
url = buildGetUrl(nvps, url);
System.out.println("URL==>" + url);
InputStream is = null;
try {
    HttpGet httpget = new HttpGet(url);
    HttpResponse response = getThreadSafeClient().execute(httpget);
...
StringdownloadJson(URI uri)
download Json
try {
    Log.i(TAG, "downloading user flair from " + uri);
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(uri);
    HttpResponse httpResponse = httpClient.execute(httpGet);
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(new GZIPInputStream(httpResponse
                    .getEntity().getContent())), 8 * 1024);
...
StringgetURLString(String urlStr)
get URL String
try {
    URL url = new URL(urlStr);
    BufferedReader in = new BufferedReader(new InputStreamReader(
            url.openStream()));
    String inputLine;
    String json = "";
    while ((inputLine = in.readLine()) != null)
        json += inputLine;
...
byte[]getBytesFromUrl(String url)
get Bytes From Url
return readInputStream(getRequest(url));
JSONObjectgetWebContent(String pvUrl)
get Web Content
URL url = null;
try {
    url = new URL(pvUrl);
} catch (MalformedURLException e) {
    e.printStackTrace();
HttpURLConnection conn;
JSONObject ret = null;
...