Gson无法检测到类变量

时间:2017-06-16 08:49:16

标签: android retrofit rx-java rx-android gson

我使用Rxjava进行改造,并使用Gson作为解析器。我的类有几个字段,我处理的重要字段是firstName,lastName,NationalCode。我不知道为什么当我从服务器得到结果时,我无法获得国家代码,它将姓氏替换为国家代码变量。我还用邮递员检查了我的网络Api,它给了我正确的结果。 这就是我所做的一切:

个人资料类:

package com.example.android.funnyapp.data.model;

import android.graphics.Bitmap;
import android.util.Base64;
import android.util.Base64OutputStream;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by root on 6/9/17.
 */

public class Profile {
    @SerializedName("first_name")
    @Expose
    private String firstName;

    @SerializedName("last_name")
    @Expose
    private String lastName;



    @SerializedName("gender")
    @Expose
    private Boolean gender;

    @SerializedName("national_code")
    @Expose
    private String nationalCode;

    public String getNationalCode() {
        return nationalCode;
    }

    public void setNationalCode(String nationalCode) {
        this.nationalCode = nationalCode;
    }
    @SerializedName("profile_id")
    @Expose
    private String profileId;
    @SerializedName("user_id")
    @Expose
    private String userId;
    @SerializedName("avatar_url")
    @Expose
    private String avatarUrl;

    @SerializedName("username")
    @Expose
    private String userName;

    @SerializedName("user_type")
    @Expose
    private String user_type;

    public Profile() {
    }

    public String getProfileId() {
        return profileId;
    }

    public void setProfileId(String profileId) {
        this.profileId = profileId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }



    public String getUserName() {
        return userName;
    }

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

    public String getUser_type() {
        return user_type;
    }

    public void setUser_type(String user_type) {
        this.user_type = user_type;
    }

    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 Boolean getGender() {
        return gender;
    }

    public void setGender(Boolean gender) {
        this.gender = gender;
    }
    private String base64;
    public String getImage() {
        return "data:image/png;base64,"+base64;
    }
    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(File avatarFile) {
        this.avatarUrl = getFileToByte(avatarFile.getAbsolutePath());
    }

    public static String getFileToByte(String path){

        try{
            InputStream inputStream=new FileInputStream(path);
            byte[] buffer=new byte[8192];
            int bytesRead;
            ByteArrayOutputStream output=new ByteArrayOutputStream();
            Base64OutputStream output64=new Base64OutputStream(output, Base64.DEFAULT);
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                output64.write(buffer, 0, bytesRead);
            }
            output64.close();
            return output.toString();
        }catch (IOException e){
            e.printStackTrace();
        }

        //encodeString = Base64.encodeToString(b, Base64.DEFAULT);
        return null;

    }
}

以及我的编辑个人资料:

public class EditUserSettingPresenter {
    View mView;
    private int mErrorCode;
    public  EditUserSettingPresenter(View view){
        mView=view;
    }

    public void updateProfile(String token,Profile profile){

        Observer<Response<Profile>> myObserver= new Observer<Response<Profile>>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
            }

            @Override
            public void onNext(@NonNull Response<Profile> profileResponse) {
                mErrorCode =profileResponse.code();
                switch (profileResponse.code()){
                    case 400:
                        mView.errorResult(404);
                        break;
                    case 401:
                        mView.errorResult(401);
                        break;
                    case 403:
                        mView.errorResult(403);
                        break;
                    case 404:
                        mView.errorResult(404);
                        break;
                    case 500:
                        mView.errorResult(500);
                        break;
                    case 201:
                    case 200:

                        mView.updateProfile(profileResponse.body());
                        break;

                    default:
                        mView.errorResult(-1);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                Log.d("ErrorMessage",e.getMessage());
                mView.errorResult(mErrorCode);
            }

            @Override
            public void onComplete() {

            }
        };


        appService service;
        service= ApiUtils.getAppService();
        service.editProfile(token,profile)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(myObserver);

    }
    public interface View{
        void updateProfile(Profile profile);
        void UpdatePassword();
        void UpdateImage();
        void errorResult(int errorCode);
    }
}

我的邮递员结果:

{
  "id": "3efc50ef-8766-4686-b3f3-c89272f6fe2d",
  "first_name": "hUSSEIN",
  "last_name": "Rezaii",
  "gender": true,
  "national_code": "99522555",
  "image": {
    "url": "/images/default/empty_profile.png",
    "thumb": {
      "url": "/images/default/empty_profile.png"
    }
  },
  "user_id": "1243b22d-ccb4-4589-a411-56ce633c87de",
  "created_at": "2017-06-09T19:19:03.057Z",
  "updated_at": "2017-06-16T08:13:29.958Z"
}

2 个答案:

答案 0 :(得分:0)

可能找不到profile_id。检查你的班级与邮差的json的区别。

这是可以用来替换gson解析器的库。

https://github.com/FasterXML/jackson

答案 1 :(得分:-1)

我不确定但是使用Gson并进行改造,您的字段应该公开吗?

尝试删除私有