Retrofit-Lite for Android - Lightweight HTTP client based on OKHttp and Retrofit.
- Add the below repository into your project level build.gradle file.
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- Add the below dependency into your module level
build.gradle
file.
dependencies {
...
implementation 'com.github.aslamanver:retrofit-lite:v2.0.6'
}
For the testing purpose we used Postman-Echo URL as APIClient.API_URL = "https://postman-echo.com"
, it can be replaced with your server address once tested.
APITask.from(context).sendGET(101, "https://your-url.com/?anyparam=anything", null, new APITask.Listener() {
@Override
public void onSuccess(int pid, int status, Map<String, String> headers, String body) {
}
@Override
public void onFailed(int pid, Exception ex) {
}
});
APITask.from(this).sendPOST(201, "https://your-url.com", "{ \"leopard\" : \"animal\" }", null, new APITask.Listener() {
@Override
public void onSuccess(int pid, int status, Map<String, String> headers, String body) {
}
@Override
public void onFailed(int pid, Exception ex) {
}
});
The object can also be sent in the sendPOST
as serialized, please refer the section 10. POST JSON Text with Object
These methods are called with the process id which is given when initiated and the status code of the response.
void onSuccess(int pid, int status, Map<String, String> headers, String body);
void onFailed(int pid, Exception ex);
Implement the APITask.Listener
with the activity class or you can directly pass into the method.
Simple GET request with parameters
APITask.from(this).sendGET(101, APIClient.API_URL + "/get?leopard=animal", null, this);
Simple GET request with request headers
Map<String, String> headers = new HashMap<>();
headers.put("leopard", "animal");
APITask.from(this).sendGET(102, APIClient.API_URL + "/headers", headers, this);
Simple test of response headers
APITask.from(this).sendGET(103, APIClient.API_URL + "/response-headers?leopard=animal", null, this);
400 Status code
APITask.from(this).sendGET(104, APIClient.API_URL + "/status/400", null, this);
Plain text on POST method
APITask.from(this).sendPOST(201, APIClient.API_URL + "/post", "Leopard is an animal", null, this);
JSON text on POST method
APITask.from(this).sendPOST(201, APIClient.API_URL + "/post", "{ \"leopard\" : \"animal\" }", null, this);
POST request with headers
Map<String, String> headers = new HashMap<>();
headers.put("leopard", "animal");
APITask.from(this).sendPOST(203, APIClient.API_URL + "/post", "{}", headers, this);
Basic autentication method with header
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Basic cG9zdG1hbjpwYXNzd29yZA==");
APITask.from(this).sendGET(204, APIClient.API_URL + "/basic-auth", headers, this);
Simple autentication method with header
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Digest username=\"postman\", realm=\"Users\", nonce=\"ni1LiL0O37PRRhofWdCLmwFsnEtH1lew\", uri=\"/digest-auth\", response=\"254679099562cf07df9b6f5d8d15db44\", opaque=\"\"");
APITask.from(this).sendGET(205, APIClient.API_URL + "/digest-auth", headers, this);
Serialize the object using GSON
and implement the APITask.Request
with the class.
public class SampleReq implements APITask.Request {
@Expose
String email;
@Expose
String password;
}
SampleReq req = new SampleReq();
req.email = "google@gmail.com";
APITask.from(this).sendPOST(206, APIClient.API_URL + "/post", req, null, this);
POST method without any parameters
APITask.from(this).sendPOST(207, APIClient.API_URL + "/post", null, this);
Custom timeout, default is 1 minute
APIClient.ConfigBuilder clientBuilder = new APIClient.ConfigBuilder()
.setTimeout(3000);
APITask.from(this, clientBuilder).sendGET(301, APIClient.API_URL + "/delay/5", null, this);
Custom host name verification
APIClient.ConfigBuilder clientBuilder = new APIClient.ConfigBuilder()
.setHostnameVerifier(new APIClient.ConfigBuilder.HostnameVerifier() {
@Override
public boolean onVerify(String hostname, SSLSession session) {
return false;
}
});
APITask.from(this, clientBuilder).sendGET(302, APIClient.API_URL + "/get", null, this);
Set the listener to null to ignore callbacks
APITask.from(this).sendGET(500, APIClient.API_URL + "/get", null, null);
Sync HTTP requests should be called inside a new thread.
APITask.SyncResponse syncResponse = APITask.from(getApplicationContext()).sendPOSTSync(201, APIClient.API_URL + "/post", "Leopard is an animal", null);
The method returns a sync object as APITask.SyncResponse syncResponse
which contains the HTTP results as below.
The response object of the Sync HTTP request
APITask.SyncResponse syncResponse
It returns true if the HTTP request is successful
syncResponse.success
Process ID
syncResponse.pid
The status code of the HTTP response
syncResponse.status
Response headers
syncResponse.headers
Body of the HTTP response
syncResponse.body
It gives an exception when the success return false
syncResponse.ex
Refer the example for Sync : Example
APIClient.ConfigBuilder clientBuilder = new APIClient.ConfigBuilder()
.setSSLSocketFactoryGenerator(new APIClient.ConfigBuilder.SSLSocketFactoryGenerator(sslSocketFactory, trustManager));
APIClient.ConfigBuilder clientBuilder = new APIClient.ConfigBuilder()
.setOkHttpClientModifier(new APIClient.ConfigBuilder.OkHttpClientModifier() {
@Override
public void onSet(okhttp3.OkHttpClient.Builder okHttpClientBuilder) {
// Modify the okHttpClientBuilder
}
});