Http Get and DefaultHttpClient : Http Connection « Network « Android






Http Get and DefaultHttpClient

    

//package com.joke.praser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

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

public class HttpUtils {

  private static final String tag = "HttpUtils";

  private static final int DEFAULT_BUFFER_SIZE = 8192;

  /**
   * ????
   * 
   * @param url
   * @return
   */
  public static String httpGet(String url) {

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
      HttpGet httpget = new HttpGet(url);
      httpget.addHeader("User-Agent", "ie 6");
      HttpResponse response = httpclient.execute(httpget);
      HttpEntity entity = response.getEntity();
      return generateString(entity.getContent());
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }

  }
  
  /**
   * ????
   * 
   * @param url
   * @return
   */
  public static InputStream httpGetIS(String url) {

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
      HttpGet httpget = new HttpGet(url);
      httpget.addHeader("User-Agent", "ie 6");
      HttpResponse response = httpclient.execute(httpget);
      HttpEntity entity = response.getEntity();
      return entity.getContent();
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }

  }

  private static String generateString(InputStream stream) {
    StringBuilder sb = new StringBuilder();
    InputStreamReader reader = null;
    try {
      reader = new InputStreamReader(stream, "utf-8");
      BufferedReader buffer = new BufferedReader(reader);
      String cur;
      while ((cur = buffer.readLine()) != null) {
        sb.append(cur + "\n");
      }

    } catch (UnsupportedEncodingException e1) {
      e1.printStackTrace();
      return null;
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    } finally {
      if (reader != null) {
        try {
          reader.close();
          reader = null;
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (stream != null) {
        try {
          stream.close();
          stream = null;
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }

    return sb.toString();
  }

}

   
    
    
    
  








Related examples in the same category

1.Http Connection
2.Using HttpGet to get the web response
3.Create Http connection
4.Http connection with
5.HttpGet and DefaultHttpClient
6.Http Post
7.Simple HTTP Request
8.Http Request Class
9.Http Get and Http Post
10.Get Text from HttpResponse
11.Http Request
12.Http Downloader
13.Is Http Url Available
14.Http Retriever
15.Receive Response from HttpURLConnection
16.Print http headers. Useful for debugging.
17.Return the base URL from the given URL. Example: http://foo.org/abc.html -> http://foo.org/
18.Send message with HttpPost
19.Get Http Stream
20.Generic Object Http Loader
21.Gets http output from URL
22.Util for Http Get
23.do Http Get Request and return the status code
24.Http Get
25.implements HttpClient
26.Get File From HTTP
27.Make all redirects to go to http in stead of https
28.New Http Client Manager
29.Http Client Manager
30.Multipart Post
31.Get Server Data
32.Yahoo News Crawler
33.Send Packet
34.Read a web page
35.parse Request Header
36.Data Send Utils
37.This class is in charge of synchronizing the events (with due dates) with Google Calendar
38.Update Favicon
39.Converts key-value pair to appropriate signature for Facebook.