Back to project page android-http.
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.
package com.markom.android.http.loader; //from ww w. j a va2 s .c o m import java.lang.reflect.Type; import java.util.Collection; import android.content.Context; 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 BaseGsonLoader} and enables parsing {@link Collection} data from json by providing {@link Type} of * parsed element. By default {@link BaseGsonLoader#parseServiceResponse(String)} methods try's to parse received * response with {@link GSONParser} immediately. In order to parse collection from custom json schema (example: meta, * pagination, data...), create custom {@link ServiceResponse} class and override this class to implement your own * parsing logic. * <p> * Example: * <p> * * <pre> * public class ExampleActivity extends FragmentActivity implements LoaderCallbacks<LoaderResponse<List<Person>> { * * @Override * public Loader<LoaderResponse<List<Person>> onCreateLoader(int id, Bundle bundle) { * Type type = new TypeToken<List<Person>>() {}.getType(); * BaseGenericLoader loader = new BaseGenericLoader(this, "www.example.com", type); * // additional loader HTTP setup like request parameters, http method etc. * return loader; * } * * @Override * public void onLoadFinished(Loader<LoaderResponse<List<Person>> loader, LoaderResponse<List<Person>> response) { * if (response.isSuccess()) { * List<Person> persons = response.getServiceResponse().getData(); * // Handle succes response * } else { * // Handle failure response * } * } * * @Override * public void onLoaderReset(Loader<LoaderResponse<List<Person>> loader) { * } * * }); * </pre> * * * @param <T> generic type of data encapsulated by {@link LoaderResponse}. * * @author Marko Milos */ public class BaseGenericLoader<T> extends BaseGsonLoader<T> { /** * Type of collection to be parsed. Needed by {@link Gson} in oreder to do parse. */ protected Type type; /** * Creates new instance of {@link BaseGenericLoader} that is going to execute HTTP GET method with no url * parameters. Use set methods provided by this class to customize http request. * * @param context used by loader. * @param url for HTTP request. * @param type represents type of collection data. Needed by {@link Gson} while maping json to objects. */ public BaseGenericLoader(Context context, String url, Type type) { super(context, url); this.type = type; } @Override protected ServiceResponse<T> parseServiceResponse(String response) throws GsonParsingException { T data = GSONParser.createObjectListFromResponse(type, response); ServiceResponse<T> serviceResponse = new ServiceResponse<T>(); serviceResponse.setData(data); return serviceResponse; } }