Skip to content

Instantly share code, notes, and snippets.

@aandis
Created September 24, 2015 07:13
Show Gist options
  • Save aandis/cbbc5adc088fb84785a1 to your computer and use it in GitHub Desktop.
Save aandis/cbbc5adc088fb84785a1 to your computer and use it in GitHub Desktop.
Code example of using Android loaders to make a web request and fetch data.
package com.firstapp.recommend.loaders;
import android.content.Context;
import android.support.v4.content.Loader;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RequestQueue;
import com.firstapp.recommend.MyApplication;
import java.util.List;
/**
* Created by gamerboy on 10/5/15.
* This class serves as the base loader which any class can extend to do a network call.
* The response is returned as a list which clients can handle in {@link #newRequest()}.
* <p/>
* Clients should override {@link #getUrl()} to set the url, {@link #newRequest()} to
* set the {@link GsonRequest} to handle the response and {@link #getUniqueRequestTag()}
* to return a unique request tag for this request.
*/
public abstract class BaseLoader extends Loader<List> {
private String requestTAG;
private List cachedData;
private RequestQueue requestQueue;
private GsonRequest gsonRequest;
/**
* Stores away the application context associated with context.
* Since Loaders can be used across multiple activities it's dangerous to
* store the context directly; always use {@link #getContext()} to retrieve
* the Loader's Context, don't use the constructor argument directly.
* The Context returned by {@link #getContext} is safe to use across
* Activity instances.
*
* @param context used to retrieve the application context.
*/
public BaseLoader(Context context) {
super(context);
requestQueue = MyApplication.getRequestQueue();
requestTAG = getUniqueRequestTag();
}
/**
* {@inheritDoc}
*/
protected void onStartLoading() {
super.onStartLoading();
//start a new request only if result is null and there is no existing request
if (cachedData == null) {
forceLoad();
} else {
// request exists, deliver the result.
super.deliverResult(cachedData);
}
}
/**
* {@inheritDoc}
*/
@Override
protected void onForceLoad() {
//start request
super.onForceLoad();
//cancel all existing requests made for the same call
cancelRequest();
gsonRequest = newRequest();
gsonRequest.setTag(requestTAG);
gsonRequest.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(gsonRequest);
}
/**
* {@inheritDoc}
*/
@Override
protected void onStopLoading() {
cancelRequest();
super.onStopLoading();
}
/**
* {@inheritDoc}
*/
@Override
protected void onReset() {
//cancel request
cancelRequest();
cachedData = null;
super.onReset();
}
/**
* {@inheritDoc}
*/
@Override
public void deliverResult(List data) {
cachedData = data;
if (isStarted()) {
super.deliverResult(data);
}
}
private void cancelRequest() {
if (requestTAG != null) {
requestQueue.cancelAll(requestTAG);
}
}
/**
* @return The url as string.
*/
public abstract String getUrl();
/**
* @return The {@link GsonRequest} to handle the response.
*/
public abstract GsonRequest newRequest();
/**
* Request tag for cancelling a given request. Be careful to not pass the same
* request tag if loading data at two different places simultaneously. Eg. a viewpager
* @return A string to uniquely identify this request.
*/
public abstract String getUniqueRequestTag();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment