2017-06-16 47 views
0

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

Profile类:

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" 
} 
+0

'avatar_url'是缺少在你的JSON响应 –

+0

我只想更新3场不是所有的人 – Sandro

+0

您GSON代码看起来不错乍一看。尝试使用Retrofit/OkHttpClient记录您的响应。 –

回答

-1

我不知道,但使用GSON和改造,你的领域应当向社会公开?

尝试下探的私处

+0

我检查公共和私人都没有工作 – Sandro

+0

你有没有尝试做一些日志,放弃所有attibutes并逐一添加他们,看看是否有一个特别是造成的问题 –

+0

当和人工合作时,我放弃无障碍(没有公开的,没有私人的),并赋予班级属性与标记 –