In my application, I work with the server, that is, I send him requests and receive answers. I use access_token for authentication on the server, and for refreshing it I have refresh_token . It so happens that during the request I can programmatically detect the swelling of my access_token programmatically and then I have a method for updating it in each activation. During the update process, I pull the refresh out of the shared_preferences and, after a successful server response, I write two new tokens to the device’s memory. So it turns out that the token update method uses this design to pull the refresh from the device’s memory:
sp = Objects.requireNonNull(MessageShow.this).getSharedPreferences("refresh_token", 0); String token = sp.getString("refresh_token", ""); As can be seen from the code, the binding goes clearly to the activation. In principle, you can make a method in the class in which the refresh will be transmitted from the activation when the call is made, and then this method will somehow return the new access and refresh . But there is a question - does this transfer of the method to a separate class make sense at all? The complete method for updating a token looks like this:
public void updateToken() { APIService mAPIService = apiService(); sp = Objects.requireNonNull(MessageShow.this).getSharedPreferences("refresh_token", 0); String token = sp.getString("refresh_token", ""); mAPIService.getNewToken(new ReqAccessToken(token)).enqueue(new Callback<ResNewTokens>() { @Override public void onResponse(@NonNull Call<ResNewTokens> call, @NonNull Response<ResNewTokens> response) { if (response.isSuccessful()) { String n_access_token = Objects.requireNonNull(response.body()).getAccess_token(); String n_refresh_token = Objects.requireNonNull(response.body()).getRefresh_token(); sp = getSharedPreferences("access_token", 0); editor = sp.edit(); editor.putString("access_token", n_access_token); editor.apply(); sp = getSharedPreferences("refresh_token", 0); editor = sp.edit(); editor.putString("refresh_token", n_refresh_token); editor.apply(); } } else { ResponseBody errorBody = response.errorBody(); try { if (Objects.requireNonNull(errorBody).string().contains("refresh_token_expired")) { logOut(); } else if (errorBody.string().contains("invalid_token")) { logOut(); } } catch (IOException e) { e.printStackTrace(); } } } @Override public void onFailure(@NonNull Call<ResNewTokens> call, @NonNull Throwable t) { } }); } that is, it turns out that you need to be very tired to make a complete transfer of the method from the activit To prevent all errors from cleaning up all the jambs and maybe everything will work out, and most likely it can be done, but I have little idea how. I hope that I have a simple task and it is also easy to solve.
UPDATE
Made a class that sort of sends a request and everything should be ok, but for some reason the class method returns nothing. Here is the class that I got:
public class ServerWorking { private String url, r_token, a_token; public ServerWorking(String url, String token_r, String token_a) { this.url = url; this.r_token = token_r; this.a_token = token_a; } public ServerWorking(String url_l,String token_r) { this.url = url_l; this.r_token = token_r; } public void updateToken() { APIService mAPIService = apiService(); mAPIService.getNewToken(new ReqAccessToken(r_token)).enqueue(new Callback<ResNewTokens>() { @Override public void onResponse(@NonNull Call<ResNewTokens> call, @NonNull Response<ResNewTokens> response) { a_token = Objects.requireNonNull(response.body()).getAccess_token(); r_token = Objects.requireNonNull(response.body()).getRefresh_token(); } @Override public void onFailure(@NonNull Call<ResNewTokens> call, @NonNull Throwable t) { } }); } private APIService apiService() { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(url) .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); return retrofit.create(APIService.class); } public String getR_token() { return r_token; } public void setR_token(String r_token) { this.r_token = r_token; } public String getA_token() { return a_token; } public void setA_token(String a_token) { this.a_token = a_token; } } I'm trying to call the getA_token and getR_token to save the tokens in the device’s memory, but an empty string is passed to me, although the request for a new token has passed.