Given a URL, establishes an HttpUrlConnection and retrieves the web page content as a InputStream, which it returns as a string. - Android Network

Android examples for Network:HTTP

Description

Given a URL, establishes an HttpUrlConnection and retrieves the web page content as a InputStream, which it returns as a string.

Demo Code


//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.content.Context;
import android.util.Log;

public class Main {
    private static final String TAG = "WebUtils";
    private static final String DEFAULT_ENCODING = "ISO-8859-1";

    /**/*  w w  w .j a  v  a  2 s .co  m*/
     * Given a URL, establishes an HttpUrlConnection and retrieves
     * the web page content as a InputStream, which it returns as
     *  a string.
     *  
     * @param myurl The URL from where the html is downloaded
     * @return the html from that url
     * @throws IOException if there is a connecting problem
     */
    public final static String downloadHtml(String myurl, Context context) {
        InputStream is = null;
        try {
            URL url = new URL(myurl);
            HttpURLConnection conn = (HttpURLConnection) url
                    .openConnection();
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            // Starts the query
            conn.connect();
            int response = conn.getResponseCode();
            if (response == -1) {
                Log.e(TAG, "The response was -1, so the connection failed");
                return "Exception";
            }
            is = conn.getInputStream();
            String contentType = conn.getContentType();
            // Convert the InputStream into a string
            String contentAsString = "";
            if (contentType.equals("text/html; charset=ISO-8859-1")) {
                contentAsString = convert(is);
            } else {
                //contentAsString = convertStreamToString(is);
                contentAsString = convert(is, "UTF-8");
            }
            return contentAsString;
        } catch (Exception e) {
            Log.e(TAG, "Problems downloading the url: " + myurl
                    + ". Exception: " + e);
            return "Exception";
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    Log.e(TAG, "Problem closing the inputstream");
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * Calls the method convert with the DEFAULT_ENCODING as an extra parameter
     * @param in the InputStream to be converted
     * @return the resulting String
     * @throws IOException
     */
    public static final String convert(final InputStream in)
            throws IOException {
        return convert(in, DEFAULT_ENCODING);
    }

    /**
     *  Converts a InputStream to String for a specific encoding
     * @param in the InputStream to be converted
     * @param encoding the type of encoding
     * @return the resulting String
     * @throws IOException
     */
    public static final String convert(final InputStream in,
            final String encoding) throws IOException {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final byte[] buf = new byte[2048];
        int rd;
        while ((rd = in.read(buf, 0, 2048)) >= 0) {
            out.write(buf, 0, rd);
        }
        return new String(out.toByteArray(), encoding);
    }
}

Related Tutorials