Retrofit 2.0解析来自相同POJO类的动态json

时间:2017-04-11 13:02:56

标签: android retrofit2 jsonschema2pojo

我有什么

我有一个服务器success response

{
  "response_code": 200,
  "status": "success",
  "message": "enqiry chat fetched successfully",
  "meta_data": {
    "count": "6"
  },
  "data": {
    "enquiries": [
    ]
  }
}

Error时,相同的API会返回

{
  "response_code": 500,
  "status": "error",
  "meta_data": {
    "count": 0
  },
  "data": [],
  "message": "Please specify all required parameter to add enquiries"
}

发生了什么

error scenario数据 JsonObject 更改为 JsonArray

我的问题

成功回复一切正常(因为我从POJO class

做了success response

在错误响应时,我的应用程序崩溃说尝试读取对象但找到了数组

我无法做什么

我无法更改back-end,因为它已经开发并且也适用于网站。

我做了什么

我用Google搜索并找到了许多我无法解决问题的解决方案

POJO

public class ReviewModel {

    @SerializedName("data")
    private Data mData;

    public Data getData() {
        return mData;
    }

    public void setData(Data data) {
        mData = data;
    }

    public class Data {

        @SerializedName("reviews")
        private List<Review> mReviews;

        public List<Review> getReviews() {
            return mReviews;
        }

        public void setReviews(List<Review> reviews) {
            mReviews = reviews;
        }

    }



    public class Review {

        @SerializedName("comment_date")
        private String mCommentDate;

        public String getCommentDate() {
            return mCommentDate;
        }

        public void setCommentDate(String comment_date) {
            mCommentDate = comment_date;
        }

        }
}

1 个答案:

答案 0 :(得分:1)

我的解决方案是使用这样的东西,

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

import java.util.List;

public class Response {

    @SerializedName("response_code")
    @Expose
    private Integer responseCode;
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("meta_data")
    @Expose
    private MetaData metaData;
    @SerializedName("data")
    @Expose
    private List<Object> data = null;
    @SerializedName("message")
    @Expose
    private String message;

    public Integer getResponseCode() {
        return responseCode;
    }

    public void setResponseCode(Integer responseCode) {
        this.responseCode = responseCode;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public MetaData getMetaData() {
        return metaData;
    }

    public void setMetaData(MetaData metaData) {
        this.metaData = metaData;
    }

    public List<Object> getData() {
        return data;
    }

    public void setData(List<Object> data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    public class MetaData {

        @SerializedName("count")
        @Expose
        private Integer count;

        public Integer getCount() {
            return count;
        }

        public void setCount(Integer count) {
            this.count = count;
        }

    }
 }

使用Gson的自定义数组适配器,

ArrayAdapter类

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

public class ArrayAdapterFactory implements TypeAdapterFactory {

    @Override
    @SuppressWarnings({"unchecked", "rawtypes"})
    public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {

        TypeAdapter<T> typeAdapter = null;
        try {
            if (type.getRawType() == List.class || type.getRawType() == ArrayList.class) {

                typeAdapter = new ArrayAdapter(gson,
                        (Class) ((ParameterizedType) type.getType())
                                .getActualTypeArguments()[0]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return typeAdapter;

    }

}

ArrayAdapterFactory类

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


class ArrayAdapter<T> extends TypeAdapter<List<T>> {

    private Class<T> adapterclass;
    private Gson gson;

    public ArrayAdapter(Gson gson, Class<T> adapterclass) {
        this.adapterclass = adapterclass;
        this.gson = gson;
    }

    @Override
    public List<T> read(JsonReader reader) throws IOException {

        List<T> list = new ArrayList<T>();

        final JsonToken token = reader.peek();
        System.out.println(token);
        // Handling of Scenario 2( Check JavaDoc for the class) :
        if (token == JsonToken.STRING || token == JsonToken.NUMBER ||
                token == JsonToken.BOOLEAN) {
            T inning = (T) gson.fromJson(reader, adapterclass);
            list.add(inning);
        } else if (token == JsonToken.BEGIN_OBJECT) {
            // Handling of Scenario 1(Check JavaDoc for the class) :
            T inning = (T) gson.fromJson(reader, adapterclass);
            list.add(inning);
        } else if (token == JsonToken.BEGIN_ARRAY) {
            reader.beginArray();
            while (reader.hasNext()) {
                @SuppressWarnings("unchecked")
                T inning = (T) gson.fromJson(reader, adapterclass);
                list.add(inning);
            }
            reader.endArray();
        }

        return list;
    }

    @Override
    public void write(JsonWriter writer, List<T> value) throws IOException {

    }
}

并注册适配器工厂,

Gson gson  = new GsonBuilder().registerTypeAdapterFactory(new ArrayAdapterFactory()).create();

这将有助于反序列化json字符串。