【问题标题】:Token Authentication not able to receive a token令牌身份验证无法接收令牌
【发布时间】:2018-08-22 07:36:32
【问题描述】:

我无法从我的 REST API 接收令牌。我也使用了拦截器,但无法访问它。 每次我测试运行应用程序时,它都会让我看到里面的吐司信息 Retrofit 的 enqueue 方法中的 onFailure() 方法 谁能帮我解决这个问题。 任何帮助表示赞赏! 这是代码。

改造:

public static final String BASE_URL = "http://192.168.0.105:8000/";
public static Retrofit retrofit = null;



public static Retrofit getApiClient(){
    if(retrofit == null){
        //OkHttp Client Instance for using Interceptors
        OkHttpClient.Builder okhttpBuilder = new OkHttpClient.Builder();
        okhttpBuilder.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {

                Request request = chain.request();
                Request.Builder newRequest = request.newBuilder().header("Authorization","token");

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


        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okhttpBuilder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

Activtiy内部的登录方法:

   private fun login(email:String, password:String) {
    val init_login : Login = Login(email,password)
    val call:Call<ResponseBody> =  apiInterface.login(init_login)
    call.enqueue(object :Callback<ResponseBody>{
        override fun onFailure(call: Call<ResponseBody>?, t: Throwable?) {
            Log.i(LOG_TAG,"Login Failed")
            toast("Login Failed")
        }

        override fun onResponse(call: Call<ResponseBody>?, response: Response<ResponseBody>?) {
            prefConfig.saveToken(response?.body()!!.string())
            Log.i(LOG_TAG, "onResponse -> "+response.body().toString())
            toast(response?.body()!!.string())
        }

    })

改装电话:

@POST("/login/")
Call<ResponseBody> login(@Body Login login);

调用中使用的登录参数:

公共类登录{

@SerializedName("username")
private String user;

@SerializedName("password")
private String password;

public Login(String user,String password){
    this.user = user;
    this.password = password;
}

}

REST API:

{
"password": [
    "This field is required."
],
"username": [
    "This field is required."
]
}

【问题讨论】:

    标签: android authentication retrofit2 http-token-authentication


    【解决方案1】:

    改变这个

    @POST("/login/")
    Call<ResponseBody> login(@Body Login login);
    

    @POST("login")
    Call<ResponseBody> login(@Body Login login);
    

    【讨论】:

    • 也不工作!!!结果总是来自 onFailure 方法。我无法找出问题所在?
    • logcat 和 toast 中的登录失败消息。而已!没有别的了。
    • HttpLoggingInterceptor 添加到您的改造中以检查此处的错误futurestud.io/tutorials/…
    • Log.i(LOG_TAG,"Login Failed") 更改为Log.e(LOG_TAG, "Login Failed", t)。它会将堆栈跟踪打印到 logcat 并为您/我们提供更好的指示,了解问题所在。
    • D/OkHttp: --> END POST (45-byte body) D/OkHttp:
    猜你喜欢
    • 2017-01-01
    • 2015-06-21
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多