rally is a simple wrapper around Google's volley and your Android network structure. The goal is to reduce the amount of boilerplate code.
####For gradle users:
Add this line to your dependencies:
dependencies {
...
// Rally
compile 'io.zeplin.rally:rally:1.0.0'
...
}
Here's how we use rally, but you can easily integrate it in your own way too!
-
Extend your Android Application class to
CoreBaseApplication
for declaring and initializingRequestQueue
andImageLoader
instances. -
Create a
BaseRequest
class that extendsCoreBaseRequest
.
Here is a simple BaseRequest
class:
public abstract class BaseRequest extends CoreBaseRequest {
@Override
protected String baseUrl() {
return "https://api.example.com";
}
/**
* @return content type of the request
*/
protected String contentType() {
return httpMethod() == Request.Method.GET ? "" : "application/json";
}
/**
* @return key value map of headers required for the request
*/
protected Map<String, String> getHeaders() {
// Add your headers here, if you need
}
}
And here is a simple POST request:
public class CreateUserRequest extends BaseRequest {
private final User mUser;
public CreateUserRequest(Context context, User user) {
super(context);
mUser = user;
}
@Override
protected int httpMethod() {
return Request.Method.POST;
}
@Override
protected String path() {
return "../user";
}
@Override
protected Class responseClass() {
return UserInfo.class;
}
@Override
protected String body() {
return new GsonBuilder().create().toJson(mUser);
}
}
<APPLICATION_CLASS>.getInstance().getRequestQueue().add(new CreateUserRequest(getActivity(), mUser) {
@Override
protected void onSuccess(Object response) {
super.onSuccess(response);
UserInfo userInfo = (UserInfo) response;
// handle your response here
}
@Override
protected void onError(VolleyError error) {
super.onError(error);
// handle your error here
}
}.create());
UserInfo
is a response class to retrieve the network response, and User
is the request class to send the request to the server.
Don't worry anything about parsing, Gson
does it in an elegant way!
CoreVolleyUtil.getMethodName
Returns the String
representation of the HTTP Methods defined in volley as integer. It is handy, when you use HTTP verbs as parameters.
- tests
- gzip support
rally is available under Apache License Version 2.0. See the LICENSE.md file for more info.
If you have questions, feel free to post an issue here on GitHub or just email us at [email protected].