Android Open Source - android-http Sync Http Client






From Project

Back to project page android-http.

License

The source code is released under:

Apache License

If you think the Android project android-http listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.loopj.android.http;
//from  ww  w  . java 2 s  .  co  m
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HttpContext;

import android.content.Context;
import android.os.Message;

public abstract class SyncHttpClient extends AsyncHttpClient {

  private int responseCode;
  /*
   * as this is a synchronous request this is just a helping mechanism to pass the result back to this method.
   * Therefore the result object has to be a field to be accessible
   */
  protected String result;
  protected AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler() {

    @Override
    void sendResponseMessage(org.apache.http.HttpResponse response) {
      responseCode = response.getStatusLine().getStatusCode();
      super.sendResponseMessage(response);
    };

    @Override
    protected void sendMessage(Message msg) {
      /*
       * Dont use the handler and send it directly to the analysis (because its all the same thread)
       */
      handleMessage(msg);
    }

    @Override
    public void onSuccess(String content) {
      result = content;
    }

    @Override
    public void onFailure(Throwable error, String content) {
      result = onRequestFailed(error, content);
    }
  };

  /**
   * @return the response code for the last request, might be usefull sometimes
   */
  public int getResponseCode() {
    return responseCode;
  }

  // Private stuff
  @Override
  protected void sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest,
      String contentType, AsyncHttpResponseHandler responseHandler, Context context) {
    if (contentType != null) {
      uriRequest.addHeader("Content-Type", contentType);
    }

    /*
     * will execute the request directly
     */
    new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler).run();
  }

  public abstract String onRequestFailed(Throwable error, String content);

  public String get(String url) {
    this.get(url, null, responseHandler);
    return result;
  }

  public String get(String url, RequestParams params) {
    this.get(url, params, responseHandler);
    /*
     * the response handler will have set the result when this line is reached
     */
    return result;
  }

  public String get(String url, Header[] headers, RequestParams params) {
    this.get(null, url, headers, params, responseHandler);
    return result;
  }

  public String post(String url) {
    this.post(url, null, responseHandler);
    return result;
  }

  public String post(String url, RequestParams params) {
    this.post(url, params, responseHandler);
    return result;
  }

  public String post(String url, HttpEntity entity, String contentType) {
    this.post(null, url, entity, contentType, responseHandler);
    return result;
  }

  public String post(String url, Header[] headers, RequestParams params, String contentType) {
    this.post(null, url, headers, params, contentType, responseHandler);
    return result;
  }

  public String post(String url, Header[] headers, HttpEntity entity, String contentType) {
    this.post(null, url, headers, entity, contentType, responseHandler);
    return result;
  }

  public String put(String url) {
    this.put(url, null, responseHandler);
    return result;
  }

  public String put(String url, RequestParams params) {
    this.put(url, params, responseHandler);
    return result;
  } 

  public String put(String url, HttpEntity entity, String contentType) {
    this.put(null, url, entity, contentType, responseHandler);
    return result;
  }

  public String put(String url, Header[] headers, HttpEntity entity, String contentType) {
    this.put(null, url, headers, entity, contentType, responseHandler);
    return result;
  }

  public String delete(String url) {
    this.delete(url, responseHandler);
    return result;
  }

  public String delete(String url, Header[] headers) {
    this.delete(null, url, headers, responseHandler);
    return result;
  }

}




Java Source Code List

com.example.loopj.ExampleUsage.java
com.example.loopj.TwitterRestClientUsage.java
com.example.loopj.TwitterRestClient.java
com.example.markom.CustomCollectionLoaderActivity.java
com.example.markom.CustomObjectLoaderActivity.java
com.example.markom.ExampleHandlersActivity.java
com.example.markom.MainActivity.java
com.example.markom.RawResponseLoaderActivty.java
com.example.markom.TestUrls.java
com.example.markom.http.handler.CustomClassHandler.java
com.example.markom.http.handler.CustomGenericHandler.java
com.example.markom.http.loader.CustomClassLoader.java
com.example.markom.http.loader.CustomGenericLoader.java
com.example.markom.http.schema.CustomServiceResponse.java
com.example.markom.http.schema.Meta.java
com.example.markom.http.schema.Pagination.java
com.example.markom.model.Person.java
com.loopj.android.http.AsyncHttpClient.java
com.loopj.android.http.AsyncHttpRequest.java
com.loopj.android.http.AsyncHttpResponseHandler.java
com.loopj.android.http.BinaryHttpResponseHandler.java
com.loopj.android.http.JsonHttpResponseHandler.java
com.loopj.android.http.PersistentCookieStore.java
com.loopj.android.http.RequestParams.java
com.loopj.android.http.RetryHandler.java
com.loopj.android.http.SerializableCookie.java
com.loopj.android.http.SimpleMultipartEntity.java
com.loopj.android.http.SyncHttpClient.java
com.markom.android.http.exceptions.GsonParsingException.java
com.markom.android.http.exceptions.ParsingNotImplementedException.java
com.markom.android.http.handler.BaseClassHandler.java
com.markom.android.http.handler.BaseGenericHandler.java
com.markom.android.http.handler.BaseGsonHandler.java
com.markom.android.http.loader.BaseClassLoader.java
com.markom.android.http.loader.BaseGenericLoader.java
com.markom.android.http.loader.BaseGsonLoader.java
com.markom.android.http.loader.LoaderResponse.java
com.markom.android.http.loader.RawResponseLoader.java
com.markom.android.http.model.HttpMethod.java
com.markom.android.http.model.ServiceResponse.java
com.markom.android.http.parser.GSONParser.java