You need to do some step before your application is running with SDK, such as :
Create an interface class
Create an interface class for API call
Java class select it as Interface and name the file as ApiService and add the below code to it. Comments are added inside the code to understand the code in more detail.
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;
public interface ApiService {
@POST("/api/status")
Call<Response> checkStatus(@Body String body);
}
Create Response Model
Create Response Model for container the return response
Java class select it as container the response and name the file as Response and add the below code to it.
public class Response {
private String status;
private String message;
public Response(String status, String message) {
super();
this.status = status;
this.message = message;
}
public String getStatus() {
return status;
}
public String getMessage() {
return message;
}
}
Working with MainActivity file
Create MainActivity file
add the below code to it. Comments are added in the code to get to know in detail.
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.json.JSONException;
import org.json.JSONObject;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import com.safous.waap.ApiProtection;
import com.safous.waap.ApiProtectionCallBack;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ApiProtection protection = new ApiProtection(getApplicationContext());
//for example setupEnv
protection.setupEnv(
"d322-1234-4859-fex9-76djo8gg",
"https://device.xxxx.xxxx.safous.com",
"https://auth.xxxx.xxxx.safous.com",
"https://device.xxxx.xxxx.safous.com",
" /data/user/0/com.xxxxx/files/xxxxx.p12",
"FTEXJnDBOBgkqhkiG9w0BBQ0wQTApBgkqhkiG9w0BBQwwHAQIBtk36xHzRZMCAggAMAwGCCqGSIb3DQIJBQAwFAYIKoZIhvcNAwcECDNAZ/FJYO+1BIIJSMixz2upnPpvx8ZeL28kOtC+PUexNfZPymiamx"
);
ApiProtectionCallBack < Object > callBack = new ApiProtectionCallBack < Object > () {
@Override
public void onSuccessReceived(Object response) {
if (response instanceof OkHttpClient) {
OkHttpClient client = (OkHttpClient) response;
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
// base url server
.baseUrl("https://example.com/")
// client containing header cookie and User-Agent
.client(client)
// this is for handling response, convert string to object
.addConverterFactory(ScalarsConverterFactory.create())
// as we are sending data in json format so
// we have to add Gson converter factory
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
// below line is to create an instance for our retrofit api class.
ApiService apiService = retrofit.create(ApiService.class);
// passing data from our text fields to our modal class.
JSONObject paramObject = new JSONObject();
try {
paramObject.put("id", "1");
} catch (JSONException e) {
e.printStackTrace();
}
// calling a method to create a post and passing our modal class.
Call < Response > call = apiService.checkStatus(paramObject.toString());
// on below line we are executing our method.
call.enqueue(new Callback < Response > () {
@Override
public void onResponse(Call < Response > call, retrofit2.Response < Response > response) {
// this method is called when we get response from our api.
}
@Override
public void onFailure(Call < Response > call, Throwable t) {
// this method is called when we can't hit or can't get response from our api.
}
});
}
}
@Override
public void onErrorReceived(com.safous.waap.utilities.Error errorResponse) {
}
};
protection.transaction(callBack);
}
}