Generic Object Http Loader : Http Connection « Network « Android






Generic Object Http Loader

    


import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

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 abstract class HttpClient<T>
{    
  /**
   * Effectue une requ?te HTTP GET et r?cup?re un <code>T</code> en retour
   * @param url L'url concern?e
   * @return Un <code>T</code> contenant la r?ponse
   * @throws IOException Si un probl?me intervient durant la requ?te
   * @throws URISyntaxException Si l'url est foireuse
   */
  public T getResponse(String url) throws IOException, URISyntaxException
  {
    DefaultHttpClient client = new DefaultHttpClient();
    InputStream data = null;
    URI uri = new URI(url);
    HttpGet method = new HttpGet(uri);
    method.setHeader("User-Agent", "Mozilla /4.0 (compatible; MSIE 6.0; Windows CE; IEMobile 7.6) Vodafone/1.0/SFR_v1615/1.56.163.8.39");

    HttpResponse response = client.execute(method);
    HttpEntity entity = response.getEntity();
    T result = null;
    if (entity != null)
    {
      try
      {
        data = entity.getContent();
        result = transformStream(data);
      }
      catch (IOException e)
      {
        throw e;
      }
      catch (RuntimeException e)
      {
        method.abort();
        throw e;
      }
      finally
      {
        if (entity != null) entity.consumeContent();
        client.getConnectionManager().shutdown();  
      }
    }
    return result;
  }
  
  protected abstract T transformStream(InputStream is) throws IOException;    
}

   
    
    
    
  








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.Http Get and DefaultHttpClient
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.