获取空响应,但okhttp显示数据

时间:2019-02-08 16:14:07

标签: android retrofit2

实际上,我正在okhttp中获取数据 D / OkHttp:{“状态”:“成功”,“数据”:{“ uuid”:“ 14bfe2f0-251f-11e9-a8f0-4bad1a9a4005”,“名字”:“ Rahul” ...

响应成功,但无法从response.body()获取数据 当我尝试从响应中获取数据时,它给出了null。 没有得到适当的解决方案。请帮助

这是我的答复

{
"status": "success",
"data": {
    "uuid": "14bfe2f0-251f-11e9-a8f0-4bad1a9a4005",
    "first_name": "Rahul",
    "last_name": "Pathak",
    "email": "errahulgr8@gmail.com",
    "gender": "male",
    "mobile_no": "",
    "profile_pic_url": null,
    "is_email_activated": 0,
    "is_mobile_no_activated": 0,
    "is_active": 1,
    "is_admin": 0,
    "created_at": "2019-01-31 06:11:46",
    "updated_at": "2019-02-08 10:13:00",
    "password_reset_code": null,
    "last_login_at": "2019-02-08 10:13:00",
    "primary_sport": null,
    "sub_primary_sport": null,
    "main_sport": null,
    "other_sports": "",
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImY4NzJmZTBhYTU1NzZlODcxZGEyODAxZGQxZDcwYmQzZTY3ZTgyYjNlYjUzMWY1NTdmODY3MGY0YmQyYmU5YjMxY2I4OTU4OTI2ZDFmNGZmIn0.eyJhdWQiOiIxIiwianRpIjoiZjg3MmZlMGFhNTU3NmU4NzFkYTI4MDFkZDFkNzBiZDNlNjdlODJiM2ViNTMxZjU1N2Y4NjcwZjRiZDJiZTliMzFjYjg5NTg5MjZkMWY0ZmYiLCJpYXQiOjE1NDk2MjA3ODAsIm5iZiI6MTU0OTYyMDc4MCwiZXhwIjoxNTgxMTU2NzgwLCJzdWIiOiIxNyIsInNjb3BlcyI6W119.dnIixtjJYAK3AV5mNVQTBmFi_pzrauRvuh3OocQnL8LwMyPW7ADobuZ1nhHPIGl4GrWOEzyGfWBelsJwUzQef79RH2JSUEKJKkoH06cLSFEDUpScQ0gMPIXJErAiZvr6B7ltNKWT77_T5EpWAU4jEeFUqbyrJQjK4EGvJiRpWg6a4iGamCQSkgHAfyr16AQl9WASBFRH2F_1EUKYVQLlxZddtbQIs6RJA9PbrepYvfv3VPmYbNU4_10riJ8P4gkjUwXsOHz97RiEKHXNQDJ-vq4h2GmS2OkG5TkF2vK62cWNHxnsBdHwiAQHZfAr2BiMBe3p69_Bo13pCgxUvOTyCTA_0W-R30xhX__hWfgVLTCAaKqJloPnVhaB602C6BcXvgTghmJ7Z7VY6-JBjZ-K8Xgs4CzOp4TGew9BODdX1GDU2e0CPht31ieHGbFBFAUY0DkndYhVVSpvnPBxPD1TA_qodOZdYPhX2DwwKpWMhA05pwDjvSzA8cCkpINVS-PsUUX1QH-5eBP5O8qR6wGTN_Cu-rgpnKZIodriGpNE-YcPGb2VphREhGLlyyTwCP1wNh657ZYTPJinZvXbafsEp1jGJyhR9vI3hEX56k_lBYs-LY4Go48IIrzRZ8CFF70IKEqQedC2QVvWPQh6kcCuQOC1F44lP6-IA43pyakzoug"
}

}

这是我的模型课

public class Model {

    @SerializedName("uuid")
    @Expose
    private String uuid;
    @SerializedName("first_name")
    @Expose
    private String firstName;
    @SerializedName("last_name")
    @Expose
    private String lastName;
    @SerializedName("email")
    @Expose
    private String email;
    @SerializedName("gender")
    @Expose
    private String gender;
    @SerializedName("mobile_no")
    @Expose
    private String mobileNo;
    @SerializedName("profile_pic_url")
    @Expose
    private Object profilePicUrl;
    @SerializedName("is_email_activated")
    @Expose
    private Integer isEmailActivated;
    @SerializedName("is_mobile_no_activated")
    @Expose
    private Integer isMobileNoActivated;
    @SerializedName("is_active")
    @Expose
    private Integer isActive;
    @SerializedName("is_admin")
    @Expose
    private Integer isAdmin;
    @SerializedName("created_at")
    @Expose
    private String createdAt;
    @SerializedName("updated_at")
    @Expose
    private String updatedAt;
    @SerializedName("password_reset_code")
    @Expose
    private Object passwordResetCode;
    @SerializedName("last_login_at")
    @Expose
    private String lastLoginAt;
    @SerializedName("token")
    @Expose
    private String token;

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getMobileNo() {
        return mobileNo;
    }

    public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }

    public Object getProfilePicUrl() {
        return profilePicUrl;
    }

    public void setProfilePicUrl(Object profilePicUrl) {
        this.profilePicUrl = profilePicUrl;
    }

    public Integer getIsEmailActivated() {
        return isEmailActivated;
    }

    public void setIsEmailActivated(Integer isEmailActivated) {
        this.isEmailActivated = isEmailActivated;
    }

    public Integer getIsMobileNoActivated() {
        return isMobileNoActivated;
    }

    public void setIsMobileNoActivated(Integer isMobileNoActivated) {
        this.isMobileNoActivated = isMobileNoActivated;
    }

    public Integer getIsActive() {
        return isActive;
    }

    public void setIsActive(Integer isActive) {
        this.isActive = isActive;
    }

    public Integer getIsAdmin() {
        return isAdmin;
    }

    public void setIsAdmin(Integer isAdmin) {
        this.isAdmin = isAdmin;
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

    public String getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(String updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Object getPasswordResetCode() {
        return passwordResetCode;
    }

    public void setPasswordResetCode(Object passwordResetCode) {
        this.passwordResetCode = passwordResetCode;
    }

    public String getLastLoginAt() {
        return lastLoginAt;
    }

    public void setLastLoginAt(String lastLoginAt) {
        this.lastLoginAt = lastLoginAt;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}

这是回调

private void loginUser() {
    userLoginId = loginId.getText().toString();
    userLoginPas = loginPassword.getText().toString();

    Call<Model> call = apiInterface.login(userLoginId, userLoginPas);
    call.enqueue(new Callback<Model>() {
        @Override
        public void onResponse(Call<Model> call, Response<Model> response) {

            if (response.isSuccessful()) {
                Model model = response.body();
                String MyResult = response.body().toString();
                Log.d("TAG", response.code() + model.getToken());
                Log.d("Data", response.body().getToken() + "");
            }
        }

        @Override
        public void onFailure(Call<Model> call, Throwable t) {
            call.cancel();
        }
    });
}

2 个答案:

答案 0 :(得分:1)

您的Model类是错误的。您可以利用泛型

   public class TheResponse<T> {
      public String status;
      public T data;
   }

,您将必须用Model替换TheResponse<Model>

答案 1 :(得分:1)

尝试此操作以从响应中获取值

Log.d("TAG", "Token" + model.getData().getToken());