【问题标题】:How to store the access token and refresh token in android studio?如何在android studio中存储访问令牌和刷新令牌?
【发布时间】:2021-10-29 03:02:10
【问题描述】:

我正在使用 volley 库为我的 android 分配实现 rest API,我想知道如何保存令牌,因为每个其他 API 都需要访问和刷新令牌,我应该将它保存在数据库中吗?调用其他 API 时如何访问存储的令牌?

这是我的 webApi 类,

 public void login(String email, String password, final APIListener listener) {
      String url = BASE_URL + "/oauth/token";
      JSONObject jsonObject = new JSONObject();

      try {
           JSONObject userJSON = new JSONObject();
           userJSON.put("email",email);
           userJSON.put("password", password);
           jsonObject.put("user",userJSON);
           jsonObject.put("grant_type", "password");

           Log.d("Json Object", jsonObject.toString());

           Response.Listener<JSONObject> successListener = new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                     try{
                          Log.d("th response",response.toString());

                         

                          Gson obj = new Gson();
                          Authentication authObj = obj.fromJson(response.toString(), Authentication.class);
                          Log.d("successObj",authObj.getSuccess());

                          listener.onLogin(authObj);
                     }
                     catch(Exception ex){
                          Log.e("Volley onResponse Error",ex.toString());
                          Toast.makeText(mApplication, "JSON exception", Toast.LENGTH_LONG).show();
                     }

                }
           };

           Response.ErrorListener errorListener = new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                     Log.e("Json Error Response",error.toString());
                     Toast.makeText(mApplication, "Invalid Login", Toast.LENGTH_LONG).show();

                }
           };

           JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, successListener, errorListener);




           mRequestQueue.add(request);
      }
      catch (JSONException exception){
           Log.e("Login exception",exception.getStackTrace().toString());
           Toast.makeText(mApplication, "JSON Exception", Toast.LENGTH_LONG).show();

      }


 }

这是活动类,

 //login
    EditText emailField = findViewById(R.id.email);
    EditText passwordField = findViewById(R.id.password);
    Button loginBtn = findViewById(R.id.LoginBtn);

    loginBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String email = emailField.getText().toString();
            String password = passwordField.getText().toString();

            final Model model = Model.getInstance(LoginActivity.this.getApplication());
            model.login(email, password, new AbstractAPIListener() {
                @Override
                public void onLogin(Authentication authentication){
               
                    if(authentication.getSuccess().equals("true")) {
                        model.setAuth(authentication);
                        Toast.makeText(LoginActivity.this, "Login success!", Toast.LENGTH_LONG).show();

                        Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                        startActivity(intent);
                    }
                    else{
                        Toast.makeText(LoginActivity.this, "Invalid Login!", Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
    });

【问题讨论】:

    标签: rest android-studio android-volley access-token refresh-token


    【解决方案1】:

    您可以将访问令牌存储在共享首选项中

     public static String gettitledecreption(Context context, String Key_name) {
        return PreferenceManager.getDefaultSharedPreferences(context).getString(Key_name, "");
    }
    
    public static void settitledecreption(Context context, String username ,String key_name) {
        SharedPreferences _sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = _sharedPreferences.edit();
        editor.putString(key_name, username);
        editor.commit();
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-16
      • 2021-09-13
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 2020-01-26
      • 1970-01-01
      • 2015-12-13
      相关资源
      最近更新 更多