Итак, я нашел нестандартное решение, которое работает. Я надеюсь, что помогу людям с этим. Я хотел максимально упростить запросы, вот так выглядит HTTP-запрос:
MainActivity. java
@OnClick(R.id.button)
@Optional
void onClickButton()
{
JsonObject params = new JsonObject();
params.addProperty("key", "value");
Call < MyModel > call = myApiService.postCustomRequest(params);
RetrofitCallback.enqueue(call, new Callback < MyModel >() {
@Override
public void onResponse(@NonNull Call<NewIncidentData> call, @NonNull Response<NewIncidentData> response) {
//Do you stuff here, no need to add custom request code
}
@Override
public void onFailure(@NonNull Call<NewIncidentData> call, @NonNull Throwable t) {
//Same here
}
});
}
RetrofitCallback. java
public class RetrofitCallback {
public static <T> void enqueue(Call<T> call, final Callback<T> callback) {
Log.i(TAG, "HTTP Request : " + call.request().url());
call.enqueue(new CustomCallback<T>(call) {
@Override
public void onResponse(Call<T> call, Response<T> response) {
super.onResponse(call, response);
if(call.isCanceled())
return;
callback.onResponse(call, response);
}
@Override
public void onFailure(Call<T> call, Throwable t) {
super.onFailure(call, t);
callback.onFailure(call, t);
}
});
}
}
CustomCallback. java
public class CustomCallback<T> implements Callback<T> {
private String TAG = "CustomCallback";
private ApiService mApiService = ConnectionsRequest.getApiService();
private Config config = Config.getInstance();
public CustomCallback(Call<T> call) {
this.call = call;
}
@Override
public void onResponse(Call<T> main_call, Response<T> response) {
//Check if the token is still valid
if (new Gson().toJson(response.body()).contains("needrefreshtoken")) {
Log.i(TAG, "Generate new token");
main_call.cancel();
RetrofitCallback.enqueue(mApiService.getToken(config.refreshtoken()), new Callback<TokenModel>() {
@Override
public void onResponse(@NonNull Call<TokenModel> call, @NonNull Response<TokenModel> response) {
config.token = response.body().getToken();
Log.i(TAG, "New token generated and saved");
retryMainRequest();
}
@Override
public void onFailure( @NonNull Call<TokenModel> call, @NonNull Throwable t) {
}
});
}
}
@Override
public void onFailure(Call<T> call, Throwable t) {
Log.e(TAG, t.toString());
}
private void retryMainRequest() {
Log.i(TAG, "Retry request");
call.clone().enqueue(this);
}
}
РЕДАКТИРОВАТЬ: я забыл код:
RetrofitClientInstance. java
public class RetrofitClientInstance {
private static Retrofit retrofit;
private static final String BASE_URL = "myurl.com";
public static Retrofit getRetrofitInstance() {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addNetworkInterceptor(new AuthInterceptor()); // I added that
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(httpClient.build())
.build();
return retrofit;
}
}
AuthInterceptor. java
public class AuthInterceptor implements Interceptor {
Config config = Config.getInstance();
@Override
public Response intercept(Chain chain) throws IOException
{
Request request = chain.request();
request = request.newBuilder()
.addHeader("X-AUTH-TOKEN", config.token).build();
return chain.proceed(request);
}
}