Making API Requests

The RequestSpec contains all the information necessary to create a HTTP request, such as the request method, headers, and URI. RequestSpecs can be created with the ActivityContract and the OAuthContract for use with the Activity API and authentication. A RequestSpec can then be used with an adapter for various Android HTTP clients.

The ApiAuthenticator takes a RequestSpec and a Credential and returns a new RequestSpec that is authenticated for use. This provides a straigthforward way for common requests to be cached and reauthenticated as tokens expire. The basic adapter example below outlines creating a ReqeustSpec for list of activities using the ActivityContract, authenticating it, and then using Google Volley to make the request.

Volley Example

// Build a basic request
RequestSpec listActivities = ActivityContract.buildListActivitiesRequestSpec();

// Authenticate with the ApiAuthenticator
listActivities =

// See Google Volley code for more information about this method.
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(listActivities.getUri().toString(), null,
        new Response.Listener<JSONObject>() {
            public void onResponse(JSONObject jsonObject) {
                Log.d(TAG, String.format("List Activities JSON:%S", jsonObject.toString()));
        new Response.ErrorListener() {
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, "Volley error" + volleyError.toString());

// Schedule the request in the Request queue
RequestQueue rq = Volley.newRequestQueue(getInstrumentation().getContext());

Api Request Task

The Nike+ Android SDK provides a lightweight networking implementationt that wraps HttpUrlConnection in an AsyncTask. This implementation uses default HTTP connection settings and does not provide advanced error handling or network status handling over the defauls of HttpUrlConnection. However it does provide an easy way to get started using the Nike+ Android SDK and reduce the level of effort required for prototyping an application.
 * Short sample of how to use an ApiRequestTask.
public class TaskExecutor extends Activity implements ApiRequestTask.ApiRequestTaskHandler {

    private ApiRequestTask mApiRequestTask;

        Add success and error handling logic. 
    public void onSuccess(Object o, HttpResponse httpResponse) {
        // Handle success

    public void onError(Object o, Exception e, HttpResponse httpResponse) {
        // Handle error

    // Create a request spec and execute it with the ApiRequestTask
    public void getListOfActivities() {
        // Create request
        RequestSpec spec = ActivityContract.buildListActivitiesRequestSpec();
        // Authenticate request
        spec = ApiAuthenticator.authenticateRequestSpec(spec, NikeFacade.getCredential(this));
        // Create an new ApiRequestTask with a handler
        this.mApiRequestTask = new ApiRequestTask(this);
        // Execute the task. The handler will be called with the result.