Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
496 views
in Technique[技术] by (71.8m points)

android - SignOut Using Retrofit and Shared Preference

I would like to do Signout function in my android application. First, I use retrofit to do login and now, I want to logout using retrofit.I am passing Authrazation Token, in header if i don't use Token in then i will get This error "getting response http/1.1, code=403, message=Forbidden,url=https:/auth/user/logout". How to do Logout using Retrofit and Also i having getting response http/1.1, code=403, message=Forbidden,url=https://auth/user/logout,even after passing token, Response body getting null,its showing. What to do?

Postman Header Fileenter image description here Bearer Passing In Header File

ProfileFregment

              signout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            SessionMaintain.init(getActivity());
            //read string in shared preference.

            String token = SessionMaintain.read(SessionMaintain.TOKEN_USER, null);
            Integer userId = SessionMaintain.read(SessionMaintain.USER_ID, 0);

    
            logout(userId,token);

        }
    });

}

private void logout(Integer userId,String authorization) {
    ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
    Call<LogResponse> logoutCall = apiInterface.signOut(userId,authorization);
    logoutCall.enqueue(new Callback<LogResponse>() {
       
       @Override
        public void onResponse(Call<LogResponse> call, Response<LogResponse> response) {

          Log.d("LogResponse",response.toString());
            if(response.isSuccessful()){

                LogResponse lresponse = response.body();
                lresponse.getMessage();



                Log.d("Token","Response"+response.body().getMessage());
                Intent i = new Intent(getActivity(), LoginActivity.class);
                startActivity(i);
                SessionMaintain.clear();


            }
        }

      

Shared Preference

public static String read(String key, String defValue) {
    return mSharedPref.getString(key, defValue);
}

public static void write(String key, String value) {
    SharedPreferences.Editor prefsEditor = mSharedPref.edit();
    prefsEditor.putString(key, value);
    prefsEditor.commit();
}

public static boolean read(String key, boolean defValue) {
    return mSharedPref.getBoolean(key, defValue);
}

public static void write(String key, boolean value) {
    SharedPreferences.Editor prefsEditor = mSharedPref.edit();
    prefsEditor.putBoolean(key, value);
    prefsEditor.commit();
}

Model

 @SerializedName("message")

public String message;

ApiInteraction

                     @FormUrlEncoded
                   @POST("auth/user/logout")
     Call<LogResponse> signOut(@Field("user_Id") Integer user_id,
                          @Header("Authorization") String authorization);
             

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Assuming that you need to clear your user session data when you got a success response from your logout http call.

Add the following function to your SessionMaintain class:

public static void clear() {
    SharedPreferences.Editor prefsEditor = mSharedPref.edit();
    prefsEditor.clear();
    prefsEditor.commit();
}

and use it when you get a success response from your log out service, like this:

@Override
public void onResponse(Call<LogResponse> call, Response<LogResponse> response)
{
    Log.d("LogResponse", response.toString());
    if (response.isSuccessful()) {
        // here
        SessionMaintain.clear()
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...