Last active
August 29, 2015 13:55
-
-
Save mbarcia/8736184 to your computer and use it in GitHub Desktop.
MyApi Volley request factory. Part of http://develop-for-android.blogspot.com.es/2014/01/using-volley-in-your-application_31.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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