Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – SerializedName注释似乎在Moshi中不起作用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我试图使用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中不起作用所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。