我无法从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."
]
}
答案 0 :(得分:0)
更改此
"javascript.implicitProjectConfig.experimentalDecorators": true
收件人
@POST("/login/")
Call<ResponseBody> login(@Body Login login);