【问题标题】:Gson can't detect the class variableGson 检测不到类变量
【发布时间】:2017-11-18 23:16:45
【问题描述】:

我使用 Rxjava 和改造,也使用 Gson 作为解析器。我的班级有几个字段,我处理的重要字段是名字、姓氏、国家代码。我不知道为什么当我从服务器获得结果时,我无法获得国家代码,它将姓氏替换为国家代码变量。我还用邮递员检查了我的网络 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"
}

【问题讨论】:

  • avatar_url 在您的 json 响应中丢失
  • 我只想更新 3 个字段而不是全部
  • 你的 Gson 代码乍一看还不错。尝试使用 Retrofit/OkHttpClient 记录您的响应。
  • 我记录了我的观察者 onNext 方法中的 retrfoti 响应,profileResponse.body() 方法值。它给出了错误的数据,我的意思是姓氏和国家代码是相同的。事实上,它们不应该是相同的。

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


【解决方案1】:

可能是未找到 profile_id。检查与您的班级和 Postman 的 json 的区别。

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

https://github.com/FasterXML/jackson

【讨论】:

    【解决方案2】:

    我不确定,但使用 Gson 和改造,你的字段应该公开吗?

    尝试删除私人信息

    【讨论】:

    • 你有没有尝试做一些日志,删除所有的属性并一一添加,看看是否有一个特别导致了问题
    • 当个人使用 Gson 时,我放弃了可访问性(无公共,无私有),并为类属性赋予与标签相同的名称
    • 我得到了日志,在我的演示者观察者中,profileResponse.body() 给出了错误的答案,它给出了国家代码和姓氏相同的名字
    • 其他都好吗?
    • 它给出了错误的数据,姓氏和国家代码不应该相同。它没有给出任何错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多