我在Spring Boot应用程序中有一个JWT身份验证。在后端,POST方法/登录返回JSON就像
BroadcastReceiver
我在Android客户端遇到此问题。我使用Retrofit 2.
在界面中我有:
{"token":"xxxxxxxxxxxxxxxxxxxxxxxxx"}
我的改造客户:
@POST("/login")
Call<TokenInfo> login(@Body UserCredentials userCredentials);
在package com.findme.my_app_android;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitClient {
private static Retrofit ourInstance;
public static Retrofit getInstance(){
if(ourInstance==null)
ourInstance = new Retrofit.Builder()
.baseUrl("http://172.20.10.2:8080")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
return ourInstance;
}
}
我使用:
MainActivity
在通话中&lt; ...&gt;我使用private void login(UserCredentials userCredentials, final View v){
Call<TokenInfo> call = restAPI.login(userCredentials);
call.enqueue(new Callback<TokenInfo>() {
@Override
public void onResponse(Call<TokenInfo> call, Response<TokenInfo> response) {
//retrofit authentication token save
if(response.isSuccessful()){
Log.d("token", response.body().token);
}
openDeviceChoiceActivity();
}
@Override
public void onFailure(Call<TokenInfo> call, Throwable t) {
openAlert(v);
}
});
}
和TokenInfo
,如下所示:
TokenHolder
或
public class TokenInfo {
@SerializedName("token")
@Expose
public String token;
}
很遗憾,我的public class TokenHolder {
private static TokenHolder instance = new TokenHolder();
private String token = null;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public static TokenHolder getInstance() {
return instance;
}
}
始终是response.body()
。有什么问题?
答案 0 :(得分:0)
检查此日志:
public void onResponse(Call<TokenInfo> call, Response<TokenInfo> response) {
Log.d("error", response.errorBody().string())
}
答案 1 :(得分:0)
你应该看到
response.code()
如果代码大于300,则客户端请求错误如下: 404-&GT;未找到 401-&GT;验证错误等 请让我知道错误代码