Submits a HTTP GET request to a given URL. - Android Network

Android examples for Network:HTTP Request

Description

Submits a HTTP GET request to a given URL.

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    /**/*from  w  ww  .j ava2  s.  c  o  m*/
     * Submits a GET request to a given URL. 
     * Given a URL, establishes an HttpUrlConnection and retrieves
     * the web page content as a InputStream, which it returns as 
     * a string.
     * 
     * Reference: <a href="http://developer.android.com/training/basics/network-ops/connecting.html">http://developer.android.com/training/basics/network-ops/connecting.html</a> On Oct 23, 2014
     * 
     * @param myurl         The url to submit the get request to.
     * @return            The response as a string.
     * @throws IOException   Exception thrown on any failure to read from url.
     */
    // Given a URL, establishes an HttpUrlConnection and retrieves
    // the web page content as a InputStream, which it returns as
    // a string.
    public static String getFromUrl(String myurl) throws IOException {
        InputStream is = null;
        try {
            URL url = new URL(myurl);
            HttpURLConnection conn = (HttpURLConnection) url
                    .openConnection();
            conn.setReadTimeout(5000 /* milliseconds */);
            conn.setConnectTimeout(3000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            // Starts the query
            conn.connect();
            //int response = conn.getResponseCode();

            // Log.d(DEBUG_TAG, "The response is: " + response);

            is = conn.getInputStream();

            // Convert the InputStream into a string
            String contentAsString = readResponse(is);
            // Log.d(DEBUG_TAG, contentAsString);
            return contentAsString;

            // Makes sure that the InputStream is closed after the app is
            // finished using it.
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }

    /**
     * Converts the input stream from a url connection into a response string.
     * 
     * Reference: <a href="http://stackoverflow.com/questions/11766878/sending-files-using-post-with-httpurlconnection">http://stackoverflow.com/questions/11766878/sending-files-using-post-with-httpurlconnection</a> on Oct 23, 2014, User Mihai Todor
     * 
     * @param stream                  The stream to read from.
     * @return                        The response as a string.
     * @throws IOException               Exception thrown on any failure to read from stream.
     * @throws UnsupportedEncodingException   Expceptino thrown on any non-recognized encoding type. 
     */
    private static String readResponse(InputStream stream)
            throws IOException, UnsupportedEncodingException {
        BufferedReader responseStreamReader = new BufferedReader(
                new InputStreamReader(stream));
        String line = "";
        StringBuilder stringBuilder = new StringBuilder();
        while ((line = responseStreamReader.readLine()) != null) {
            stringBuilder.append(line).append("\n");
        }
        responseStreamReader.close();

        String response = stringBuilder.toString();
        return response;
    }
}

Related Tutorials