I have several questions related to the solution of one problem. The point is quite simple: in my application I send requests to the server and get answers from it, and it turns out that sometimes I get a token that needs to be updated right away. The method of updating tokens is found in almost every activity in which the request is sent, and 10 copies have already been collected. I also have many other methods that can be put into a separate class and, if necessary, drag data from it. In my most recent question, kind people ( @ Yuriy SPb, thanks to me ) advised me to use Singleton to solve this problem.
Googling for a while, I realized that he was right and the singletons would suit me perfectly. For this, I created a separate class that will contain all the methods that I want to endure. During the discussion in the comments, I was advised to use the Application Singleton and take context from it. If I understood correctly, then I will need to transfer the context of the activation from which I call my singleton to singleton. It seems like everything is clear, but not very. Here at me in my singlton the class is used:
public synchronized static MySingleton getInstance() { if (mInstance == null) { synchronized (MySingleton.class) { if (mInstance == null) mInstance = new MySingleton(); } } return mInstance; } which is supposed to give me back my context but this does not happen. After the update of tokens, I want to write my new tokens in sharedpreferences, this is what my function of updating tokens in a singleton looks like:
private static void updateToken(String r_token) { APIService mAPIService = apiService(url); mAPIService.getNewToken(new ReqAccessToken(r_token)).enqueue(new Callback<ResNewTokens>() { @Override public void onResponse(@NonNull Call<ResNewTokens> call, @NonNull Response<ResNewTokens> response) { if (response.isSuccessful()) { acc_token = Objects.requireNonNull(response.body()).getAccess_token(); ref_token = Objects.requireNonNull(response.body()).getRefresh_token(); // error /*sharedPreferences = PreferenceManager.getDefaultSharedPreferences(); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("access_token", acc_token); editor.apply(); editor.putString("refresh_token", acc_token); editor.apply();*/ } else { ResponseBody errorBody = response.errorBody(); try { if (Objects.requireNonNull(errorBody).string().contains("refresh_token_expired")) { //logOut(); } } catch (IOException e) { e.printStackTrace(); } } } @Override public void onFailure(@NonNull Call<ResNewTokens> call, @NonNull Throwable t) { } }); } and this is how my whole singleton class looks like:
public class MySingleton { private static MySingleton mInstance; private SharedPreferences sharedPreferences; private static String url; private static String ref_token; private static String acc_token; private static String appl_name, appl_lname, a_id; public synchronized static MySingleton getInstance() { if (mInstance == null) { synchronized (MySingleton.class) { if (mInstance == null) mInstance = new MySingleton(); } } return mInstance; } private MySingleton() { Log.w("MY_TAG", "MySingleton"); } public MySingleton(String url_l, String token_r, String token_a) { url = url_l; ref_token = token_r; acc_token = token_a; } public String getAcc_token() { return acc_token; } public void setAcc_token(String acc_token) { MySingleton.acc_token = acc_token; } public String getAppl_name() { return appl_name; } public String getAppl_lname() { return appl_lname; } public String getA_id() { return a_id; } private static void updateToken(String r_token) { APIService mAPIService = apiService(url); mAPIService.getNewToken(new ReqAccessToken(r_token)).enqueue(new Callback<ResNewTokens>() { @Override public void onResponse(@NonNull Call<ResNewTokens> call, @NonNull Response<ResNewTokens> response) { if (response.isSuccessful()) { acc_token = Objects.requireNonNull(response.body()).getAccess_token(); ref_token = Objects.requireNonNull(response.body()).getRefresh_token(); // error /*sharedPreferences = PreferenceManager.getDefaultSharedPreferences(); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("access_token", acc_token); editor.apply(); editor.putString("refresh_token", acc_token); editor.apply();*/ } else { ResponseBody errorBody = response.errorBody(); try { if (Objects.requireNonNull(errorBody).string().contains("refresh_token_expired")) { //logOut(); } } catch (IOException e) { e.printStackTrace(); } } } @Override public void onFailure(@NonNull Call<ResNewTokens> call, @NonNull Throwable t) { } }); } public static void getPersonalData() { final APIService mAPIService = apiService(url); mAPIService.getData("Bearer " + acc_token).enqueue(new Callback<UserInfo>() { @Override public void onResponse(@NonNull Call<UserInfo> call, @NonNull Response<UserInfo> response) { if (response.isSuccessful()) { appl_name = Objects.requireNonNull(response.body()).getApp_f_name(); appl_lname = Objects.requireNonNull(response.body()).getApp_l_name(); a_id = String.valueOf(Objects.requireNonNull(response.body()).getApp_id()); } else { ResponseBody errorBody = response.errorBody(); try { if (Objects.requireNonNull(errorBody).string().contains("access_token_expired")) { updateToken(ref_token); getPersonalData(); } if (errorBody.string().contains("invalid_token")) { getPersonalData(); } if (errorBody.string().contains("refresh_token_expired")) { //logOut(); } } catch (IOException e) { e.printStackTrace(); } } } @Override public void onFailure(@NonNull Call<UserInfo> call, @NonNull Throwable t) { } }); } /*private void logOut() { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); sharedPreferences.edit().remove("access_token").apply(); sharedPreferences.edit().remove("refresh_token").apply(); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean("hasVisited", false).apply(); Intent intent = new Intent(mInstance, LoginActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); context.startActivity(intent); }*/ public static APIService apiService(String url) { 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); } } Most likely in the process of creating my singleton I had errors, but I can’t figure out where. Here's an example of how I create my singleton from activites:
ms = new MySingleton(url, refresh_token, access_token); MySingleton.getPersonalData(); it seems that I managed to update my tokens from it, but beyond that I could not advance. I can not understand where and what I did wrong.
Singleton.getInstance()should be the only way to get its single instance. - woesssmInstancecannot do withoutstatic. - woesssMySingleton.getInstance().get/set<Что-то>- woesss am