2017-06-06 28 views
0

我使用retrofit2在應用程序註銷,但每次它給error406 :不接受:用戶是未登錄。 。我正在使用改進自定義 標頭認證。這是我的代碼:機器人改造後註銷請求給錯誤406無法接受:用戶沒有登錄...上郵遞員API工作正常

註銷代碼

公共無效註銷()

{ Log.v( 「checkTokenbefore」,Constants.token);

OkHttpClient httpClient1 = new OkHttpClient.Builder().addInterceptor(new Interceptor() { 
     @Override 
     public Response intercept(Interceptor.Chain chain) throws IOException { 
      Request original = chain.request(); 
      Log.v("checkLogin",Constants.token+Constants.username+Constants.password) ; 
      // Request customization: add request headers 
      Request.Builder requestBuilder = original.newBuilder() 
        .addHeader("Accept-Language","application/json").addHeader("content-type", "application/x-www-form-urlencoded") 
        .addHeader("API_KEY", "a5XSE8XCdsY6hAoCNojYBQ") 


        .addHeader("X-CSRF-Token",Constants.token) 


        ; 

      Request request = requestBuilder.method(original.method(),original.body()).build(); 
      return chain.proceed(request); 
     } 
    }).build(); 

    Retrofit retrofit1 = new Retrofit.Builder() 
      .baseUrl(Constants.API_BASE_URL) 
      .client(httpClient1) 

      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 

    ApiInterface restAPI1 = retrofit1.create(ApiInterface.class); 

    Call<Logout> callLogout = restAPI1.userLogout(Constants.token,Constants.username,Constants.password); 
    callLogout.enqueue(new Callback<Logout>() { 
     @Override 
     public void onResponse(Call<Logout> call, retrofit2.Response<Logout> response) { 
      Log.v("responseLogout",response.code()+"code"+response.errorBody().toString()+response.message()) ; 
     } 

     @Override 
     public void onFailure(Call<Logout> call, Throwable t) { 

     } 
    }); 

}

雖然下面是用於登錄的代碼工作正常:

public void loginQuestin(){ 

    //checkValidation(); 
/* 
    ApiInterface apiService = 
      ApiClient.create(ApiInterface.class) ;*/ 
    ApiInterface restAPI = retrofit.create(ApiInterface.class); 

    Call<UserAgain> call = restAPI.userLogin(mEmailAddress.getText().toString().trim(), 
      mPassword.getText().toString().trim()); 
    call.enqueue(new Callback<UserAgain>() { 
     @Override 
     public void onResponse(Call<UserAgain> call, Response<UserAgain> response) { 
     Log.v("check",response.code()+"login"+response.body().getToken()) ; 
     //response.body().getU 
      Constants.username = mEmailAddress.getText().toString().trim() ; 
      Constants.password = mPassword.getText().toString().trim() ; 

      if (response.code()==200) { 
      Log.v("checkAgain",response.code()+"login") ; 
      Constants.token = response.body().getToken() ; 
      startActivity(new Intent(LoginActivity.this, NavigationDrawerActivity.class)); 
      } 
     } 

     @Override 
     public void onFailure(Call<UserAgain> call, Throwable t) { 
     Log.v("check","failed"); 
     t.printStackTrace(); 
     } 
    }); 
    } 

// API/HTTP客戶端進行登錄API調用

public class ApiClient { 

    public static OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() { 
     @Override 
     public Response intercept(Interceptor.Chain chain) throws IOException { 
      Request original = chain.request(); 

      // Request customization: add request headers 
      Request.Builder requestBuilder = original.newBuilder() .addHeader("Accept-Language","application/json") 
        .addHeader("content-type", "application/x-www-form-urlencoded").addHeader("API_KEY", "a5XSE8XCdsY6hAoCNojYBQ") 

        ; 

      Request request = requestBuilder.build(); 
      return chain.proceed(request); 
     } 
    }).build(); 



    public static Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(Constants.API_BASE_URL) 
      .client(httpClient) 

      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 
    public static ApiInterface restAPI = retrofit.create(ApiInterface.class); 
} 

API接口類

@POST("token") 
Call<Token> getToken(); 


@FormUrlEncoded 
@POST("login") 
Call<UserAgain> userLogin(@Field("username") String param1, @Field("password") String param2); 

@FormUrlEncoded 
@POST("logout") 
Call<Logout> userLogout(@Field("username") String param1 , @Field("password") String param2); 

登錄API的工作給予罰款200 OK響應代碼。 https://futurestud.io/tutorials/retrofit-add-custom-request-header API格式:

用戶驗證/登錄

目的:與註銷API添加動態customn頭(客戶端XSRF令牌)

參考工作時遇到的主要問題 - 用戶登錄其餘URL: -/api/v1/people/login 方法:-POST標題:Accept-Language:application/json API_KEY: a5XSE8XCdsY6hAoCNojYBQ內容類型:application/x-www-form-urlencoded X-CSRF -Token:

用戶註銷

用途: - 用戶註銷休息網址: -/API/V1 /人/註銷 方法:-POST頭:接受語言:應用程序/ JSON API_KEY: a5XSE8XCdsY6hAoCNojYBQ內容 - 類型:application/x-www-form-urlencoded X-CSRF-Token:正文中的參數:用戶名:例如 [email protected]密碼:eg 123456

回答

0

使用攔截器添加動態標題。https://futurestud.io/tutorials/retrofit:用新的令牌

Response response = chain.proceed(originalRequest); //perform request, here original request will be executed 

    if (response.code() == 401) { 
      //if unauthorized 
      //perform all 401 in sync blocks 

     } 
     return chain.proceed(newRequest.build()); 
}); 
+0

我想我已經使用了同樣的事情參考鏈接

httpClient.addInterceptor((Interceptor.Chain chain) -> { Request originalRequest = chain.request(); 

組的OAuth令牌

Request.Builder newRequest = originalRequest.newBuilder(); newRequest.header("Authorization", accessToken).method(originalRequest.method(), originalRequest.body()); originalRequest = newRequest.build(); chain.proceed(originalRequest); 

重複請求-add-custom-request-header – Naman

+0

是的..這對我自定義標題。 –

+0

我想我的XSRF令牌存在一些問題,因爲沒有API調用工作 – Naman

相關問題