使用Android中的Retrofit注销

时间:2017-02-01 08:49:57

标签: android retrofit

我必须从我的应用中退出。

这是我的php脚本:

public function logoutAction(Request $request){
   $requestData=json_decode($request->getContent(),1);
   $em=$this->get('doctrine')->getEntityManager();
   if(isset($requestData['token'])){
       if($userToken=$em->getRepository('NavZUserBundle:UserToken')->findOneBy(array('token'=>$requestData['token']))){
           $em->remove($userToken);
           $em->flush($userToken);
           $response['status']='success';
           $response['msg']="Logged out successfully";
       }else{
           $response['status']='fail';
           $response['msg']="Mobile User is already logged out";
       }
   }else{
       $response['status']='fail';
       $response['msg']="Missing required parameter";
   }
   return new JsonResponse($response);
}

这是使用POST参数的api调用:

@POST("api/logout/")
Call<ApiResponse> logOutUser(@Query("token") String token);

我有退出按钮。

@OnClick(R.id.tv_signout)
void onSignOut(View view) {
    logOutMethod();
}
private void logOutMethod() {
    ApiService apiService = RestClient.getClient();
    Call<ApiResponse> logOut = apiService.logOutUser(getPreference().getToken());
    Log.e(TAG, "logOutMethod one: "+getPreference().getToken());
    Log.e(TAG, "logOutMethod two: "+logOut );
    logOut.enqueue(new Callback<ApiResponse>() {
        @Override
        public void onResponse(Response<ApiResponse> response) {
            if (response.isSuccess()){
                ApiResponse result = response.body();
                if (result.getData() != null){
                    goToLogInActivity();
                    Snackbar.make(findViewById(android.R.id.content), result.getMsg(), Snackbar.LENGTH_LONG).show();
                }else {
                    Snackbar.make(findViewById(android.R.id.content), result.getMsg(), Snackbar.LENGTH_LONG).show();
                }
            }else {
                Snackbar.make(findViewById(android.R.id.content), response.message(), Snackbar.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Throwable t) {

        }
    });
}
private void goToLogInActivity(){
    getPreference().removeLoginPreferences();
    Intent intent = new Intent(this, LoginActivity.class);
    finish();
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    overridePendingTransition(0, 0);
    startActivity(intent);
}

调试后:

E/SettingsActivity: logOutMethod one: f7177163c833dff4b38fc8d2872f1ec658940cbf280ce
E/SettingsActivity: logOutMethod two: retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@ca3eb18

但部分原因:

ApiResponse result = response.body();

方法response.body()抛出:

data = null
msg = "Missing required parameter"
status = "fail"

在我的应用中也会抛出SnackBar消息Missing required parameter,并且应用无法退出。

使用POST方法注销可以做些什么?这可以做什么?

2 个答案:

答案 0 :(得分:0)

我想你可能得不到&#34;令牌&#34;

的价值
 Call<ApiResponse> logOut = apiService.logOutUser(new PreferenceTypes(this).getToken());

在进行退出API调用之前,请检查您是否正在清除首选项。

答案 1 :(得分:0)

我的错误:我正在尝试解析@query()而不是@Body

这就是我解决问题的方法:

我必须以token格式提交json。因此,我使用如下方法实现我的Log Out API:

现在,使用POST方法的API调用为:

@POST("/api/logout/")
Call<ApiResponse> logOutUser(@Body TokenJsonObject tokenJsonObject);

TokenJsonObject模特课:

public class TokenJsonObject {

    public TokenJsonObject(String token) {
        this.token = token;
    }

    @SerializedName("token")
    private String token;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}

最后,在sign_out按钮点击后:

 @OnClick(R.id.tv_signout)
void onSignOut(View view) {
    logOutMethod();
}

private void logOutMethod() {
    ApiService apiService = RestClient.getClient();
    Call<ApiResponse> logOut = apiService.logOutUser(new TokenJsonObject(getPreference().getToken()));
    logOut.enqueue(new Callback<ApiResponse>() {
        @Override
        public void onResponse(Response<ApiResponse> response) {
            if (response.isSuccess()) {
                ApiResponse result = response.body();
                if (result.getData() != null) {
                    Snackbar.make(findViewById(android.R.id.content), result.getMsg(), Snackbar.LENGTH_LONG).show();
                } else {
                    goToLogInActivity();
                    Snackbar.make(findViewById(android.R.id.content), result.getMsg(), Snackbar.LENGTH_LONG).show();
                }
            } else {
                Snackbar.make(findViewById(android.R.id.content), response.message(), Snackbar.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Throwable t) {

        }
    });
}

private void goToLogInActivity() {
    getPreference().removeLoginPreferences();
    Intent intent = new Intent(this, LoginActivity.class);
    finish();
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    overridePendingTransition(0, 0);
    startActivity(intent);
}