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

android – SerializedName注释似乎在Moshi中不起作用,第1张

概述所以我试图使用Moshi从我的服务器解析一个调用.这是我的回复对象. public class TokenResponse { @SerializedName("accessToken") public String accessToken; public String token_type; public int expires_in; public St 所以我试图使用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<TokenResponse> 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<TokenResponse> call = service.getToken("admin@admin.com","password1!","password");        call.enqueue(new Callback<TokenResponse>() {            @OverrIDe            public voID onResponse(Call<TokenResponse> call,Response<TokenResponse> 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不起作用

解决方法 @Serializedname(“accesstoken”)是Gson

它应该是

@Json(名称= “的access_token”)

总结

以上是内存溢出为你收集整理的android – SerializedName注释似乎在Moshi中不起作用全部内容,希望文章能够帮你解决android – SerializedName注释似乎在Moshi中不起作用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1134038.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存