从改造响应中获取错误的数据类型

时间:2019-10-01 05:16:37

标签: android retrofit2

我从Retrofit2响应中得到userId作为double,但是在服务器端的响应中,它是string

改造响应

{
    "UserId": 1.0,
    "UserName": "suresh",
    "SiteCode": 0.0,
    "FullName": "suresh, suresh",
    "Employeecode": 0.0,
    "IsActive": 0.0,
    "ProfileURL": ""
}

服务器响应

{
    "UserId": "1",
    "UserName": "suresh",
    "SiteCode": 0.0,
    "FullName": "suresh, suresh",
    "Employeecode": 0.0,
    "IsActive": 0.0,
    "ProfileURL": ""
}

我为什么会这样,请问原因和解决方法

如何获得以下答复?

{
    "UserId": "1",
    "UserName": "suresh",
    "SiteCode": 0.0,
    "FullName": "suresh, suresh",
    "Employeecode": 0.0,
    "IsActive": 0.0,
    "ProfileURL": ""
}

这是我的老客户

public static Retrofit getClient() {
        try {
            retrofit = null;
            if (retrofit == null) {

                OkHttpClient client = new OkHttpClient.Builder()
                        .connectTimeout(100, TimeUnit.SECONDS)
                        .readTimeout(100,TimeUnit.SECONDS).build();

                         retrofit = new Retrofit.Builder()
                        //.baseUrl("http://192.168.1.138/OMS_Orders/").client(client)
                        .baseUrl("someURL").client(client)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
            }
            return retrofit;
        } catch (Exception ex) {
            Log.d("Exceptionerror", ex.toString());
        }
        return retrofit;
    }

和pojo类代码是

public class LoginDTO {
 @SerializedName("SiteCode")
    private String SiteCode;

    @SerializedName("FullName")
    private String FullName;

    @SerializedName("Employeecode")
    private String Employeecode;

    @SerializedName("UserId")
    private String UserId;
}

2 个答案:

答案 0 :(得分:0)

确保您的POJO数据类如下所示

public class DataList {

@SerializedName("UserId")
@Expose
private String userId;
@SerializedName("UserName")
@Expose
private String userName;
@SerializedName("SiteCode")
@Expose
private Double siteCode;
@SerializedName("FullName")
@Expose
private String fullName;
@SerializedName("Employeecode")
@Expose
private Double employeecode;
@SerializedName("IsActive")
@Expose
private Double isActive;
@SerializedName("ProfileURL")
@Expose
private String profileURL;

public String getUserId() {
return userId;
}

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

答案 1 :(得分:0)

服务器以userIdstring的方式给您响应。 String 的值用双引号括起来,即。 (“”)。

现在,如果要在服务器响应中获得double的{​​{1}}值,则必须更改用php或其他方式编写的后端代码。

但是您可以轻松为响应创建POJO类,例如:

userId

,只要您需要将public class Response { private String UserName; private String SiteCode; private String UserId; private String Employeecode; private String IsActive; private String FullName; private String ProfileURL; public String getUserName () { return UserName; } public void setUserName (String UserName) { this.UserName = UserName; } public String getSiteCode () { return SiteCode; } public void setSiteCode (String SiteCode) { this.SiteCode = SiteCode; } public String getUserId () { return UserId; } public void setUserId (String UserId) { this.UserId = UserId; } public String getEmployeecode () { return Employeecode; } public void setEmployeecode (String Employeecode) { this.Employeecode = Employeecode; } public String getIsActive () { return IsActive; } public void setIsActive (String IsActive) { this.IsActive = IsActive; } public String getFullName () { return FullName; } public void setFullName (String FullName) { this.FullName = FullName; } public String getProfileURL () { return ProfileURL; } public void setProfileURL (String ProfileURL) { this.ProfileURL = ProfileURL; } @Override public String toString() { return "Response [UserName = "+UserName+", SiteCode = "+SiteCode+", UserId = "+UserId+", Employeecode = "+Employeecode+", IsActive = "+IsActive+", FullName = "+FullName+", ProfileURL = "+ProfileURL+"]"; } } 加倍,就可以userId