Velocity
An easy to use networking library for Android
Features
- Simplified networking
- Supports GET/POST/PUT/DELETE (for now)
- Supports file downloads and uploads (with progress callbacks)
- Supports http post data (json, multipart, url-encoded, plain text, xml)
- Simple OAuth logins using password resource flow
- Queued requests
- Global and per request mocking
- Built in deserialization for JSON (using gson)
- Handles redirects
- Adjustable settings : read/request timeout, mock delay, global simulated delay, max redirects, user agent, multipart boundary
- Installation Add the following to your project's main gradle file:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the following to your app.gradle file
implementation 'com.github.ravindu1024:velocity:<latest-version>'
Usage
Velocity uses a background threadpool to execute network requests and it needs to initialized at app launch:
Velocity.initialize(3); //initialize the threadpool with background threads (eg: 3 or 6 or whatever you feel like). All threads will be waiting on a queue.
Simple GET request:
Velocity.get("http://www.google.com")
.connect(new Velocity.ResponseListener()
{
@Override
public void onVelocityResponse(Velocity.Response response)
{
if(response.isSuccess)
{
//...
}
}
});
GET request with headers and path parameters:
Velocity.get(url)
.withHeader("header1", "value1")
.withHeader("header2", "value2")
.withPathParam("path1", "value1")
.withPathParam("path2", "value2")
.connect(new Velocity.ResponseListener()
{
@Override
public void onVelocityResponse(Velocity.Response response)
{
if(response.isSuccess)
{
//...
}
}
});
POST with form data:
Velocity.post(url)
.withFormData("key1", "value1")
.withFormData("key2", "value2")
.connect(new Velocity.ResponseListener()
{
@Override
public void onVelocityResponse(Velocity.Response response)
{
if(response.isSuccess)
{
//...
}
}
});
Queuing multiple requests: When multiple requests are queued and executed all replies are provided in a single callback. If one or more requests fail, the whole queued request is considered failed. The multi-response queue is threadsafe and is global to the application process.
Velocity.get(url).queue(0);
Velocity.download(file).setDownloadFile(filepath).queue(1);
Velocity.get(randomImage).queue(2);
Velocity.executeQueue(new Velocity.MultiResponseListener()
{
@Override
public void onVelocityMultiResponse(HashMap<Integer, Velocity.Response> responseMap)
{
if(responseMap.get(0).isSucess)
{
//.....
}
//.....
}
});
Deserialization:
MyWrapperClass data = serverResponse.deserialize(MyWrapperClass.class);