Android Http Get sendGetRequest(String path)

Here you can find the source of sendGetRequest(String path)

Description

send Get Request

Declaration

public static String sendGetRequest(String path) throws Exception 

Method Source Code

//package com.java2s;
import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    public static String sendGetRequest(String path) throws Exception {
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5 * 1000);
        conn.setRequestMethod("GET");
        InputStream inStream = conn.getInputStream();
        byte[] data = readInputStream(inStream);
        String result = new String(data, "UTF-8");
        return result;
    }/* w  ww  .  j a  va  2 s.c  om*/

    public static byte[] readInputStream(InputStream inStream)
            throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();
        outStream.close();
        inStream.close();
        return data;
    }
}

Related

  1. httpGetRequestParseParams( String paramString)
  2. formatHttpHeaders(Map headers)
  3. getStringResponseData(HttpResponse httpResponse)
  4. getHttpClient()
  5. maybeCreateHttpClient()
  6. fetchData(String url)