Android Open Source - android-http Base Generic Handler






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.markom.android.http.handler;
/*from  ww w .j a  v a 2 s .  co  m*/
import java.lang.reflect.Type;
import java.util.Collection;

import com.google.gson.Gson;
import com.markom.android.http.exceptions.GsonParsingException;
import com.markom.android.http.model.ServiceResponse;
import com.markom.android.http.parser.GSONParser;

/**
 * Extends {@link BaseGsonHandler} and enables parsing {@link Collection} data from json by providing {@link Type} of
 * parsing element. By default {@link BaseGsonHandler#parseServiceResponse(String)} methods try's to parse received
 * response with {@link GSONParser} immediately. In order to parse collection from custom json schema, create custom
 * {@link ServiceResponse} class and override this class to implement your own parsing logic.
 * <p>
 * Example:
 * 
 * <pre>
 * Type type = new TypeToken&lt;List&lt;Person&gt;&gt;() {
 * }.getType();
 * 
 * client.get(&quot;www.example.com&quot;, new BaseGenericHandler&lt;List&lt;Person&gt;&gt;(type) {
 * 
 *   &#064;Override
 *   public void onStart() {
 *     // started
 *   }
 * 
 *   &#064;Override
 *   public void onSuccess(int statusCode, Header[] headers, ServiceResponse&lt;List&lt;Person&gt;&gt; result) {
 *     super.onSuccess(statusCode, headers, result);
 *     List&lt;Person&gt; persons = result.getData();
 *     // handle success
 *   }
 * 
 *   &#064;Override
 *   public void onFailure(Throwable error, String content) {
 *     super.onFailure(error, content);
 *     // handle failure
 *   }
 * 
 *   &#064;Override
 *   public void onFinish() {
 *     // finished
 *   }
 * 
 * });
 * </pre>
 * 
 * @param <T> generic type of data encapsulated by {@link ServiceResponse}.
 * 
 * @author Marko Milos
 */
public class BaseGenericHandler<T> extends BaseGsonHandler<T> {

  /**
   * Type of collection to be parsed. Needed by {@link Gson} in oreder to do parse.
   */
  protected Type type;

  /**
   * Creates an new instance of {@link BaseGenericHandler}.
   * 
   * @param type the type of collection requested.
   */
  public BaseGenericHandler(Type type) {
    this.type = type;
  }

  @Override
  protected ServiceResponse<T> parseServiceResponse(String response) throws GsonParsingException {
    // Create collection using GSONParser
    T data = GSONParser.createObjectListFromResponse(type, response);

    // Create new ServiceResponse object and set parsed data
    ServiceResponse<T> serviceResponse = new ServiceResponse<T>();
    serviceResponse.setData(data);
    return serviceResponse;
  }

}




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