android-Retrofit 2,同名不同数据类型的JSON解析

android-Retrofit 2,同名不同数据类型的JSON解析,第1张

概述我正在尝试从服务器解析JSON响应,如果对post方法中发送的查询进行了更改,则我将第一个作为响应,否则,我将第二个作为响应.1:{"status":1,"data":{"firstname":"FirstName","lastname":"LastName","mobilenumber

我正在尝试从服务器解析JSON响应,如果对post方法中发送的查询进行了更改,则我将第一个作为响应,否则,我将第二个作为响应.

1:

{    "status": 1,    "data": {        "firstname": "First name",        "lastname": "Last name",        "mobilenumber": "1234567894",        "emailID": "test@gmail.com",        "timezone": "Asia/Kolkata"    },    "user_ID": "",    "response": "Profile Updated Successfully"}

2:

{    "status": 1,    "data": "No changes to update",    "user_ID": ""}

如您所见,如果有更改,数据将返回一个对象,如果没有更改,则数据将以字符串形式返回.

我正在使用这种方法来获取数据,并且我正在使用Gson Converter来映射数据.

这是请求界面

@FormUrlEncoded@POST("pondguard/updateprofile")Call<UserResponse> getInfoUpdated(@FIEld("user_ID") String user_ID,                                  @FIEld("firstname") String firstname,                                  @FIEld("lastname") String lastname,                                  @FIEld("mobilenumber") String mobileNumber,                                  @FIEld("emailID") String emailID)

这是我的POJO课

public class UserResponse implements Parcelable {    public static final Creator<UserResponse> CREATOR = new Creator<UserResponse>() {        @OverrIDe        public UserResponse createFromParcel(Parcel in) {            return new UserResponse(in);        }        @OverrIDe        public UserResponse[] newArray(int size) {            return new UserResponse[size];        }    };    private String status;    private Data data;    private String response;    private String error;    protected UserResponse(Parcel in) {        status = in.readString();        data = in.readParcelable(Data.class.getClassLoader());        response = in.readString();    }    @OverrIDe    public int describeContents() {        return 0;    }    @OverrIDe    public voID writetoParcel(Parcel dest, int flags) {        dest.writeString(status);        dest.writeParcelable(data, flags);        dest.writeString(response);    }    public String getStatus() {        return status;    }    public Data getData() {        return data;    }    public String getResponse() {        return response;    }    public String getError() {        return error;    }}

最后我进行了改造电话:

Retrofit retrofit = new Retrofit.Builder()        .baseUrl(ConstantUtils.BASE_URL)        .addConverterFactory(GsonConverterFactory.create())        .build();UserInfoRequestInterface requestInterface = retrofit.create(UserInfoRequestInterface.class);Call<UserResponse> call = requestInterface.getInfoUpdated(user_ID, firstname, lastname, phoneNumber, email, null, null);

解决方法:

谢谢您的建议,但我想出了一种可行的方法.这是我的方法…

首先,在我的Pojo类中,添加了一个JsonDeserializer,然后检查“数据”是对象还是基元,并根据其设置相应的字段.

public class UserResponse  {    @Serializedname("status")    private String status;    @Serializedname("data")    private Object mData;    @Serializedname("response")    private String response;    @Serializedname("error")    private String error;    private String message;    private String firstname;    private String lastname;    private String mobilenumber;    private String emailID;    private String timezone;    public String getMessage() {        return message;    }    public voID setMessage(String message) {        this.message = message;    }    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 getMobilenumber() {        return mobilenumber;    }    public voID setMobilenumber(String mobilenumber) {        this.mobilenumber = mobilenumber;    }    public String getEmailID() {        return emailID;    }    public voID setEmailID(String emailID) {        this.emailID = emailID;    }    public String getTimezone() {        return timezone;    }    public voID setTimezone(String timezone) {        this.timezone = timezone;    }    public String getStatus() {        return status;    }    public voID setStatus(String status) {        this.status = status;    }    public Object getmData() {        return mData;    }    public String getResponse() {        return response;    }    public String getError() {        return error;    }    public static class DataStateDeserializer implements JsonDeserializer<UserResponse> {        @OverrIDe        public UserResponse deserialize(JsonElement Json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {            UserResponse userResponse = new Gson().fromJson(Json, UserResponse.class);            JsonObject JsonObject = Json.getAsJsonObject();            if (JsonObject.has("data")) {                JsonElement elem = JsonObject.get("data");                if (elem != null && !elem.isJsonNull()) {                                         if(elem.isJsonPrimitive()){                                                    userResponse.setMessage(elem.getAsstring());                    }else{                        userResponse.setFirstname(elem.getAsJsonObject().get("firstname").getAsstring());                        userResponse.setLastname(elem.getAsJsonObject().get("lastname").getAsstring());                        userResponse.setMobilenumber(elem.getAsJsonObject().get("mobilenumber").getAsstring());                        userResponse.setEmailID(elem.getAsJsonObject().get("emailID").getAsstring());                        userResponse.setTimezone(elem.getAsJsonObject().get("timezone").getAsstring());                    }                }            }            return userResponse ;        }    }}

然后将Json解串器附加到GSON Builder的类型适配器,并像这样在Retrofit中给它提供GsonConvertor的创建方法

Gson gson = new GsonBuilder()                 .registerTypeAdapter(UserResponse.class, new UserResponse.DataStateDeserializer())                .create();Retrofit retrofit = new Retrofit.Builder()                .baseUrl(ConstantUtils.BASE_URL)                .addConverterFactory(GsonConverterFactory.create(gson))                .build();UserInfoRequestInterface requestInterface = retrofit.create(UserInfoRequestInterface.class);        Call<UserResponse> call = requestInterface.getInfoUpdated(user_ID, firstname, lastname, phoneNumber, email, null, null);

然后,我要做的就是检查message是否为null并相应地执行所需的 *** 作.

总结

以上是内存溢出为你收集整理的android-Retrofit 2,同名不同数据类型的JSON解析全部内容,希望文章能够帮你解决android-Retrofit 2,同名不同数据类型的JSON解析所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1094782.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存