【问题标题】:How to POST retrofit return JSON with Root如何使用 Root POST 改造返回 JSON
【发布时间】:2021-09-15 18:00:46
【问题描述】:

我在使用 root/parent 获取 json 时遇到问题

网络服务

@POST("login")
Call<LoginResponse> userLogin(@Body LoginRequest loginRequest);

我的响应模型

public class LoginResponse {
    @SerializedName("token")
    private String tokenLogin;
    @SerializedName("token_type")
    private String tokenType;
    @SerializedName("expires_in")
    private String expiresTime;
}

我的电话回复:

  Call<LoginResponse> loginResponseCall = 
  ApiClient.getUserService().userLogin(loginRequest);
    loginResponseCall.enqueue(new Callback<LoginResponse>() {
        @Override
        public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
            if(response.isSuccessful()){
                token = response.body().getTokenLogin().toString();
                String tokenType = response.body().getTokenType();
                String tokenExpire = response.body().getExpiresTime();
                userDao.addUser(new User(etUsername.getText().toString().trim(),etPassword.getText().toString().trim(),token));
}

当我的回答是:

{ “令牌”:“123456”, "token_type": "类型", “expire_in”:“60” }

但需要采取以下措施: { 用户:{ “令牌”:“123456”, "token_type": "类型", “expire_in”:“60” } }

我需要做什么?

【问题讨论】:

    标签: android post retrofit


    【解决方案1】:

    所以你已经创建了一个名为 User 的新类成为该参数。试试这个

    public class LoginResponse {
        @SerializedName("users")
        private User user;
    }
    

    创建另一个模型类。

    public class User {
        @SerializedName("token")
        private String tokenLogin;
        @SerializedName("token_type")
        private String tokenType;
        @SerializedName("expires_in")
        private String expiresTime;
    }
    

    【讨论】:

    • 谢谢,我懂逻辑,但是我需要如何修改 POST 方法和调用?
    • 为什么要修改POST方法和Call? @AndrewS
    • 我需要另一种方法,但是我在调​​用 List 时遇到了同样的问题:@POST("products") Call> products(@Body ProductRequest productRequest);对不起,如果我没有正确解释ю
    • 你试过答案了吗? @AndrewS
    • 是的,这个问题没问题。现在我创建另一个问题。谢谢
    猜你喜欢
    • 2021-09-15
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 2015-05-09
    • 1970-01-01
    相关资源
    最近更新 更多