Authentication 改装2招摇过市响应为200,但无法从响应主体获取令牌
我尝试从一个招摇过市的restful服务获取连接并使用改装2获取令牌,我得到的响应是200,但无法从响应主体获取令牌Authentication 改装2招摇过市响应为200,但无法从响应主体获取令牌,authentication,retrofit,swagger,token,Authentication,Retrofit,Swagger,Token,我尝试从一个招摇过市的restful服务获取连接并使用改装2获取令牌,我得到的响应是200,但无法从响应主体获取令牌 private void login() { Call<User> call=userClient.login("user","123","password"); call.enqueue(new Callback<User>() { @Override public voi
private void login() {
Call<User> call=userClient.login("user","123","password");
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if(response.isSuccessful()){
token=response.body().getToken();
Toast.makeText(LoginActivity.this,token,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(LoginActivity.this,"login not correct",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
Toast.makeText(LoginActivity.this,"error",Toast.LENGTH_SHORT).show();
}
});
}
private void login(){
Call Call=userClient.login(“用户”、“123”、“密码”);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
token=response.body().getToken();
Toast.makeText(LoginActivity.this,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(LoginActivity.this,“登录不正确”,Toast.LENGTH_SHORT.show();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(LoginActivity.this,“error”,Toast.LENGTH_SHORT.show();
}
});
}
像这样创建用户类
public class User {
@SerializedName("access_token")
@Expose
private String accessToken;
@SerializedName("token_type")
@Expose
private String tokenType;
@SerializedName("expires_in")
@Expose
private Integer expiresIn;
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getTokenType() {
return tokenType;
}
public void setTokenType(String tokenType) {
this.tokenType = tokenType;
}
public Integer getExpiresIn() {
return expiresIn;
}
public void setExpiresIn(Integer expiresIn) {
this.expiresIn = expiresIn;
}
}
token=response.body().getAccessToken();
然后像这样试试
public class User {
@SerializedName("access_token")
@Expose
private String accessToken;
@SerializedName("token_type")
@Expose
private String tokenType;
@SerializedName("expires_in")
@Expose
private Integer expiresIn;
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getTokenType() {
return tokenType;
}
public void setTokenType(String tokenType) {
this.tokenType = tokenType;
}
public Integer getExpiresIn() {
return expiresIn;
}
public void setExpiresIn(Integer expiresIn) {
this.expiresIn = expiresIn;
}
}
token=response.body().getAccessToken();
您的改装实例应该如下所示
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.your_baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
你试过记录
response
的值吗?响应是:response{protocol=http/1.1,code=200,message=OK,url=}。response.body()的值是多少?我使用了postman,得到的结果和令牌似乎没有什么异常,但在我的应用程序中尝试时,我的令牌为空作为响应复制来自邮递员的响应并在此处显示感谢您这么多的工作,我的用户模型格式不正确很好,很高兴提供帮助!