Skip to content

Instantly share code, notes, and snippets.

@mbarcia
Last active August 29, 2015 13:55
Show Gist options
  • Save mbarcia/8736184 to your computer and use it in GitHub Desktop.
Save mbarcia/8736184 to your computer and use it in GitHub Desktop.
package net.colaborativa.exampleapp;
import android.content.Context;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.Volley;
import net.colaborativa.exampleapp.api.LoginRequest;
import net.colaborativa.exampleapp.api.LoginResponse;
import net.colaborativa.exampleapp.api.volley.GsonPRequest;
public class MyApi {
private final RequestQueue mQueue;
public static final String BASE_URL =
"http://127.0.0.1:8080/exampleapp";
public static final String LOGIN_URL =
BASE_URL + "/login";
private static final int POST_TIMEOUT =
60000; // 1 min, in miliseconds
public MyApi(Context context) {
mQueue = Volley.newRequestQueue(context);
}
public Request<?> newLoginRequest(
String username,
String password,
Listener<LoginResponse> listener,
ErrorListener errorListener) {
// Handle the inputs to the server, in this case,
// is creating the input DAO object with user/pwd
LoginRequest payload =
new LoginRequest(username, password);
//
// Call the request adapter.
//
// This adapter GsonPRequest was created to POST/PUT
// a JSON body and accept a JSON response. It will
// accept any class as request/response DAOs, and will
// "talk" JSON with them using the Gson library.
//
// The request payload is a DAO (LoginRequest) with
// username and password.
//
// The response is expected to be a DAO (LoginResponse)
// containing the session token.
//
GsonPRequest<LoginRequest, LoginResponse> req =
new GsonPRequest<LoginRequest, LoginResponse>(
LOGIN_URL,
LoginRequest.class,
payload,
LoginResponse.class,
listener,
errorListener
);
// set retry policy
req.setRetryPolicy(new DefaultRetryPolicy(
POST_TIMEOUT,
3,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// add the request to the queue and return it
return mQueue.add(req);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment