request By HTTP Get via URL - Android Network

Android examples for Network:HTTP Request

Description

request By HTTP Get via URL

Demo Code


//package com.java2s;

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

import android.util.Log;

public class Main {
    private static final int HTTP_200 = 200;
    private static final String TAG_GET = "Web GET";

    public static void requestByGet(String path) throws Exception {
        URL url = new URL(path);
        HttpURLConnection urlConn = (HttpURLConnection) url
                .openConnection();/*from   w  w  w. j ava  2s.co m*/
        urlConn.setConnectTimeout(5 * 1000);
        urlConn.connect();
        if (urlConn.getResponseCode() == HTTP_200) {
            byte[] data = readStream(urlConn.getInputStream());
        } 
        urlConn.disconnect();
    }

    public static byte[] readStream(InputStream inStream) throws Exception {
        int count = 0;
        while (count == 0) {
            count = inStream.available();
        }
        byte[] b = new byte[count];
        inStream.read(b);
        return b;
    }
}

Related Tutorials