【问题标题】:Retrofit JSON GET request returning nullRetrofit JSON获取请求返回NULL
【发布时间】:2020-06-13 00:22:31
【问题描述】:

改造 JSON GET 请求为嵌套字段返回 null。如何访问 UserID 字段数据?我究竟做错了什么 ?我不知道如何使用改造来解析 json。熟悉使用 Retrofit 解析简单的 json,但不熟悉使用 Retrofit 解析嵌套的 Json。

JSON 数据:

{
    "Status": "Success",
    "ErrorMessage": null,
    "ErrorKey": null,
    "RedirectUrl": null,
    "Data": {
        "UserDetails": {
            "UserID": "d7a6397c-fa33-4268-a64a-b99d0e78dfcf"
        }
    }
}

GetData 接口:

@GET("Account/LoginAPi")
    Call<LoginResponse> getUser(@Query("UserName") String username,
                                    @Query("Password") String password);

POJO:

public class LoginResponse {

    @SerializedName("UserName")
    String UserName;

    @SerializedName("Password")
    String Password;

    @SerializedName("UserID")
    String UserID;

    @SerializedName("Status")
    String Status;



    public LoginResponse(String UserName, String Password, String UserID, String Status)
    {
        this.UserName = UserName;
        this.Password = Password;
        this.UserID = UserID;
        this.Status = Status;
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String Password){
        this.Password = Password;
    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String UserName){
        this.UserName = UserName;
    }

    public String getUserID() {
        return UserID;
    }

    public void setUserID(String UserID) {
        this.UserID = UserID;
    }

    public String getStatus() {
        return Status;
    }

    public void setStatus(String Status) {
        this.Status = Status;
    }
}

主活动:

GetData service = RetrofitClient.getRetrofitInstance().create(GetData.class);

            Call<LoginResponse> call =  service.getUser(username, password);

            call.enqueue(new Callback<LoginResponse>() {
                @Override
                public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
                    pDialog.dismiss();

                    try {
                        String userid = response.body().getUserID();
                        toast(userid);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFailure(Call<LoginResponse> call, Throwable t) {

                }
            });

【问题讨论】:

    标签: android json retrofit retrofit2


    【解决方案1】:

    您的登录响应类与您提供的 json 不匹配,这是您的问题。

    我通常使用jsonschema2pojo网站轻松解析json文件

    把你的json粘贴进去:

    -------------- Data --------------------
    public class Data {
    
     @SerializedName("UserDetails")
     public UserDetails userDetails;
    
    }
    -------------- LoginResponse ------------
    
    package com.example;
    
    import com.google.gson.annotations.SerializedName;
    
    public class LoginResponse {
    
    @SerializedName("Status")
    public String status;
    @SerializedName("ErrorMessage")
    public Object errorMessage;
    @SerializedName("ErrorKey")
    public Object errorKey;
    @SerializedName("RedirectUrl")
    public Object redirectUrl;
    @SerializedName("Data")
    public Data data;
    
    }
    -----------------------------------com.example.UserDetails.java-----------------------------------
    
    package com.example;
    
    import com.google.gson.annotations.SerializedName;
    
    public class UserDetails {
    
    @SerializedName("UserID")
    public String userID;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-12
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 2017-11-06
      • 2017-11-26
      • 2011-05-30
      相关资源
      最近更新 更多