android 过期注释,android – SerializedName注释似乎在Moshi中不起作用

在尝试使用Retrofit和Moshi从服务器获取TokenResponse时,博主遇到一个问题:虽然响应包含所有字段,但使用SerializedName注解的字段在解析后总是为null。问题集中在 accessToken, .issued 和 .expires 字段上,其他字段能正常获取。博主已经确认服务器返回的JSON中包含了这些字段。目前尚不清楚是序列化问题还是其他原因导致的解析失败。
摘要由CSDN通过智能技术生成

所以我试图使用Moshi从我的服务器解析一个调用.这是我的回复对象.

public class TokenResponse {

@SerializedName("accessToken")

public String accessToken;

public String token_type;

public int expires_in;

public String userName;

public String name;

@SerializedName(".issued")

public String issued;

@SerializedName(".expires")

public String expires;

public String Roles;

}

这是我的端点定义(不是很重要,但无论如何我都会包含它)

public interface ServerService {

@POST("/token")

@FormUrlEncoded

Call getToken(@Field("username") String username,

@Field("password") String password, @Field("grant_type") String grant_type);

}

这是我用来调用的代码.

Retrofit retrofit = new Retrofit.Builder()

.baseUrl("https://xxx/")

.addConverterFactory(MoshiConverterFactory.create())

.build();

ServerService service = retrofit.create(ServerService.class);

Call call = service.getToken("admin@admin.com", "password1!", "password");

call.enqueue(new Callback() {

@Override

public void onResponse(Call call, Response response) {

if (response.isSuccessful()) {

// tasks available

TextView tv = (TextView)findViewById(R.id.tvToken);

tv.setText(response.body().accessToken);

} else {

// error response, no access to resource?

}

}

});

在onResponse方法中,我的response.body()始终具有accessToken,已发布并且过期为null.我得到其他参数的值.使用Android Profiler,我肯定知道它会将此作为响应返回.

{

"access_token":"_xxx",

"token_type":"bearer",

"expires_in":1209599,

"userName":"xxx",

"name":"LOURDES RILEY",

".issued":"Tue, 19 Dec 2017 23:37:06 GMT",

".expires":"Tue, 02 Jan 2018 23:37:06 GMT",

"Roles":"[\"Admin\"]"

}

那么我做错了什么?为什么SerializedName不起作用?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值