Showing posts with label Synchronous. Show all posts
Showing posts with label Synchronous. Show all posts

Thursday, 30 May 2013

Use AndroidHttpClient Instead of DefaultHttpClient

HTTP client for Android providing both synchronous (blocking) and asynchronous interfaces so it can be used on or off the UI thread.

Sample usage:

**********************

Synchronous (for use off the UI thread in an AsyncTask or Runnable)

AndroidHttpClient httpClient = new AndroidHttpClient("http://www.google.com");
    ParameterMap params = httpClient.newParams().add("q", "GOOG");
    HttpResponse httpResponse = httpClient.get("/finance", params);
    System.out.println(httpResponse.getBodyAsString());
 
**********************
Asynchronous (can be used anywhere, automatically wraps in an AsyncTask)

AndroidHttpClient httpClient = new AndroidHttpClient("http://www.google.com");
    ParameterMap params = httpClient.newParams().add("q", "GOOG");
    httpClient.setMaxRetries(3);
    httpClient.get("/finance", params, new AsyncCallback() {
        public void onComplete(HttpResponse httpResponse) {
            System.out.println(httpResponse.getBodyAsString());
        }
        public void onError(Exception e) {
            e.printStackTrace();
        }
    });