this is an WIP api, developed to make specific REST calls easy and fast to code.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.MansourM.ez-server-api:{$networkLibrary}:{$version}'
}
"fan" uses Fast Android Networking for networking
@deprecated
"ion" uses ION for networking (more utility but needs google play service to work)
implementation 'com.github.MansourM.ez-server-api:fan:0.6.2'
implementation 'com.github.MansourM.ez-server-api:ion:0.1.5
G.java
application class
public class G extends Application {
public static Context APP_CONTEXT;
public static ServerApi API;
@Override
public void onCreate() {
super.onCreate();
APP_CONTEXT = getApplicationContext();
API = new ServerApi(APP_CONTEXT);
API.getConfig()
.setLogging(true)
.setLoggingTag("Mansour_API")
.setHeaderAcceptJson();
API.setErrorMessageHandler(new MyApiCallbackDefaultHandler());
}
public static void message(String msg) {
Toast.makeText(APP_CONTEXT, msg, Toast.LENGTH_SHORT).show();
}
private static class MyApiCallbackDefaultHandler extends BaseApi.CallbackDefaultHandler {
@Override
public void handleErrorMessage(String message, JsonObject data) {
G.message("Server error: " + message);
}
@Override
public void handleFailure() {
G.message("Connection Error!");
}
}
}
G.API.request("http://192.168.1.22:8084/api/v1/app/ping")
.setMethod(Request.Method.GET)
.setRequestJason(new JsonBuilder("key1", "value1").add("key2", "value2").build())
.setRequestTimeout(10) //seconds
.setHeaderAcceptJson() // adds "Accept" -> "application/json header
.addToken("public") //adds value to "token" header
.addHeader("custom", "header")
.setCustomCallback(new SerfApiCallback() {
@Override
public void onSuccess(String message, JsonObject response) {
//response code is //200-299
}
@Override
public void onErrorMessage(String message, JsonObject response) {
//response code is //400-499
}
@Override
public void onResponse() {
//request in finished
//used for thins like loading(false)
}
@Override
public void onFailure() {
// response code is 500+
//no proper response
//e.g. no internet or parse error
}
});
AR.java
//Api Request Helper class
public class AR {
public static String getBaseIp() {
return AppSettings.getInstance().getServerIp();
}
public static void setBaseIp(String baseIp) {
AppSettings.getInstance().setServerIp(baseIp);
}
public static String getBasePort() {
return AppSettings.getInstance().getServerPort();
}
public static void setBasePort(String port) {
AppSettings.getInstance().setServerPort(port);
}
public static String baseUrl() {
String Prefix = "https://";
if (BuildConfig.DEBUG && AppSettings.getInstance().useHttpDebug())
Prefix = "http://";
return Prefix + getBaseIp() + ":" + getBasePort() + "/api/v1";
}
public static class App {
public static String baseUrl() {
return AR.baseUrl() + "/app";
}
public static UrlHelper ping() {
return UrlHelper.get(baseUrl() + "/ping");
}
public static UrlHelper sendCode() {
return UrlHelper.post("http://192.168.1.11:8084/api/v1/app/sendCode");
}
}
}
SomeActivity.java
//There are custom callbacks based on what we need this one uses success callback
// (the only callback we can access in onSuccess, onError and onfailure use the default bahviour defined in G.java)
G.API.request(AR.App.ping())
.setCustomCallback((message, response) -> {
});