Http Client Manager : Http Connection « Network « Android






Http Client Manager

    
//package com.filmatchs.utils;

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient; //
//import org.json.JSONArray;
//import org.json.JSONException;
//import org.json.JSONObject;

import android.util.Log;

/**
 * 
 * @author dycode (khalifavi)
 * 
 */
public class HttpClientManager {

  public static final String TAG = "RestClient";

  public static final int METHOD_GET = 0;
  public static final int METHOD_POST = 1;
  public static final int METHOD_PUT = 2;
  public static final int METHOD_DELETE = 4;

  //private String postfix = "";
  private String result = null;

  public HttpClientManager(String url) throws IOException,
      ClientProtocolException {
    this.result = connectResponse(url);
  }

  public HttpClientManager(String url, int method) throws IOException,
      ClientProtocolException {
    this.result = connectResponse(url, method);
  }

  public HttpClientManager(String url, int method, String postfix)
      throws IOException, ClientProtocolException {
    this.result = connectResponse(url, method, postfix);
  }

  public String getResult() {
    return this.result;
  }

  private static String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
      while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        is.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return sb.toString();
  }

  /**
   * 
   * @param url
   *            URL untuk melakukan GET dengan mengembalikan JSON String
   * @return String pengembalian nilai dari HTTP Request
   */
  public static String connectResponse(String url) throws IOException,
      ClientProtocolException {
    return connectResponse(url, METHOD_GET, null);
  }

  /**
   * 
   * @param url
   *            untuk mengambil JSON
   * @param method
   *            untuk dilakukan (GET, POST, PUT, DELETE)
   * @return String pengembalian nilai dari HTTP Request
   */
  public static String connectResponse(String url, int method)
      throws IOException, ClientProtocolException {
    return connectResponse(url, method, null);
  }

  /**
   * 
   * @param url
   *            alamat URL
   * @param method
   *            untuk dilakukan (GET, POST, PUT, DELETE)
   * @param returnType
   *            tipe dari pengembalian (JSON, XML)
   * @return String pengembalian nilai dari HTTP Request
   */
  public static String connectResponse(String url, int method, String postfix)
      throws IOException, ClientProtocolException {
    HttpClient httpClient = new DefaultHttpClient();

    if (postfix == null) {
      postfix = "";
    }

    String retval = "";

    url += postfix;

    HttpResponse response = null;

    if (method == METHOD_POST) {
      // -------------------------------- if POST
      response = httpClient.execute(new HttpPost(url));
    } else if (method == METHOD_PUT) {
      // -------------------------------- if PUT
      response = httpClient.execute(new HttpPut(url));
    } else if (method == METHOD_DELETE) {
      // -------------------------------- if DELETE
      response = httpClient.execute(new HttpDelete(url));
    } else {
      // -------------------------------- default GET
      response = httpClient.execute(new HttpGet(url));
    }

    Log.i(TAG, response.getStatusLine().toString());

    HttpEntity entity = response.getEntity();

    if (entity != null) {

      InputStream instream = entity.getContent();
      String result = convertStreamToString(instream);
      retval = result;
      instream.close();
    }

    return retval;
  }

}

   
    
    
    
  








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.Http Get and DefaultHttpClient
22.Gets http output from URL
23.Util for Http Get
24.do Http Get Request and return the status code
25.Http Get
26.implements HttpClient
27.Get File From HTTP
28.Make all redirects to go to http in stead of https
29.New 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.