Android Http Get fetchData(String url)

Here you can find the source of fetchData(String url)

Description

fetch Data

License

Open Source License

Declaration

public static String fetchData(String url) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;

public class Main {
    public static String fetchData(String url) throws Exception {
        System.out.println("Fetching data from:");
        System.out.println(url);/*from  ww  w  .j  a v a  2s. c o m*/
        InputStream inputStream = null;
        String result = "";
        Exception error = null;

        try {
            DefaultHttpClient httpclient = new DefaultHttpClient(
                    new BasicHttpParams());
            HttpGet httpget = new HttpGet(new URL(url).toURI());
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            inputStream = entity.getContent();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            result = sb.toString();
        } catch (Exception e) {
            error = e;
        } finally {
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (Exception squish) {
                //
            }
        }

        if (error != null) {
            throw error;
        }

        return result;
    }
}

Related

  1. formatHttpHeaders(Map headers)
  2. getStringResponseData(HttpResponse httpResponse)
  3. getHttpClient()
  4. maybeCreateHttpClient()
  5. sendGetRequest(String path)