I am somewhat new to android development and okhttp/Retrofit
Please check my first post for the reason why I need to try and get a retry mechanism working My First post
I need some help implementing the .clone() for Retrofit in my service so that it can try to call my URL again if it fails the first time.
my urlcontroller.java
public class UrlController{
public static boolean loading = false;
private static final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.MINUTES)
.writeTimeout(60, TimeUnit.MINUTES)
.readTimeout(60, TimeUnit.MINUTES)
.callTimeout(60, TimeUnit.MINUTES)
.build();
public static String IP_ADDRESS = "https://e***edd****.com/";//Enter You Ip_Address here here
public static String Purchase_code = "********************";
public static String Custom_Security = "***************";//Enter the Custom Security code here
// Please don't change the below code without proper knowledge
private static final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
public static String Base_URL = IP_ADDRESS + "json/appr/v1/";
private static final Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(Base_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create());
private static Retrofit retrofit = builder.build();
public static <S> S createService(Class<S> serviceClass) {
return retrofit.create(serviceClass);
}
public static <S> S createService(
Class<S> serviceClass, String username, String password, Context context) {
if (!TextUtils.isEmpty(username)
&& !TextUtils.isEmpty(password)) {
String authToken = Credentials.basic(username, password);
return createService(serviceClass, authToken, context);
} else {
return createService(serviceClass);
}
}
public static <S> S createServiceNoTimeout(Class<S> serviceClass, String authToken, Context context) {
if (!TextUtils.isEmpty(authToken)) {
AuthenticationInterceptor interceptor = new AuthenticationInterceptor(authToken, context);
if (!httpClient.interceptors().contains(interceptor)) {
httpClient.addInterceptor(interceptor);
httpClient.connectTimeout(0, TimeUnit.MINUTES);
httpClient.readTimeout(0, TimeUnit.SECONDS);
httpClient.writeTimeout(0, TimeUnit.SECONDS);
builder.client(httpClient.build());
retrofit = builder.build();
}
}
return retrofit.create(serviceClass);
}
public static <S> S createServiceNoTimeoutUP(
Class<S> serviceClass, String username, String password, Context context) {
if (!TextUtils.isEmpty(username)
&& !TextUtils.isEmpty(password)) {
String authToken = Credentials.basic(username, password);
return createServiceNoTimeout(serviceClass, authToken, context);
}
return createService(serviceClass);
}
public static <S> S createService(
Class<S> serviceClass, final String authToken, Context context) {
if (!TextUtils.isEmpty(authToken)) {
AuthenticationInterceptor interceptor =
new AuthenticationInterceptor(authToken, context);
if (!httpClient.interceptors().contains(interceptor)) {
httpClient.addInterceptor(interceptor);
builder.client(httpClient.build());
retrofit = builder.build();
}
}
return retrofit.create(serviceClass);
}
public static Map<String, String> AddHeaders(Context context) {
Map<String, String> map = new HashMap<>();
*THIS INFO WAS REMOVED FOR SECURITY PURPOSES
return map;
}
public static Map<String, String> UploadImageAddHeaders(Context context) {
Map<String, String> map = new HashMap<>();
*THIS INFO WAS REMOVED FOR SECURITY PURPOSES
return map;
}
}
My splashScreen.java
I Removed some lines of code which basically just gets the data the was returned from the call to my webpage which then gets implemented in my app it is basic data like names, color-codes and user login/signup data
public void api_getSettings() {
RestService restService = UrlController.createService(RestService.class);
try{
Call<ResponseBody> myCall = restService.getSettings(UrlController.AddHeaders(this));
myCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> responseObj) {
try {
System.out.println("try2");
if (responseObj.isSuccessful()) {
Log.d("info settings Responce", "" + responseObj.toString());
JSONObject response = new JSONObject(responseObj.body().string());
if (response.getBoolean("success")) {
jsonObjectSetting = response.getJSONObject("data");
//some stuff have been hidden for security reasons
} else {
Toast.makeText(activity, response.get("test message").toString(), Toast.LENGTH_SHORT).show();
}
}
} catch (JSONException | IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
} catch (
ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}