杰森数据
[
{
"date": "25-09-2016",
"_id": {
"$oid": "57e601bbf2a9d92a445ef8a3"
},
"createdAt": "2016-09-24 10:01:55 +0530",
"horoscope": [
{
"date": "25-09-2016",
"sunsign": "aries",
"horoscope": "Testing.."
},
{
"date": "25-09-2016",
"sunsign": "taurus",
"horoscope": "Testing.."
},
{
"date": "25-09-2016",
"sunsign": "pisces",
"horoscope": "Testing.."
}
]
}
]
HoroscopeModel.java:
public class HoroscopeModel {
private Date date;
private Date createdAt;
private List<HoroscopeModel> horoscope;
public List<HoroscopeModel> getHoroscope() {
return horoscope;
}
public void setHoroscope(List<HoroscopeModel> horoscope) {
this.horoscope = horoscope;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public List<HoroscopeModel> getHoroscopeList() {
return horoscope;
}
public void setHoroscopeList(List<HoroscopeModel> horoscopeList) {
this.horoscope = horoscopeList;
}
}
星座运势
public class Horoscope {
Date date;
String sunshine;
String horoscope;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getSunShine() {
return sunshine;
}
public void setSunShine(String sunShine) {
this.sunshine = sunShine;
}
public String getHoroscope() {
return horoscope;
}
public void setHoroscope(String horoscope) {
horoscope = horoscope;
}
}
ApiInterface.java
public interface ApiInterface {
@GET("/get_horoscope")
Call<List<HoroscopeModel>> getHoroscope();
}
改造:
private void setupRestClient() {
OkHttpClient.Builder client = new OkHttpClient.Builder();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
client.addInterceptor(loggingInterceptor);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create(getGson()))
.build();
REST_CLIENT = retrofit.create(ApiInterface.class);
}
private Gson getGson() {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, new DateDeserializer());
builder.setPrettyPrinting();
return builder.create();
}
DateDeserializer.java
public class DateDeserializer implements JsonDeserializer<Date> {
private static final String TAG = "DateDeserializer";
private static final String[] DATE_FORMATS = new String[]{
"yyy-MM-dd hh:mm:ss Z",//2016-09-24 08:18:56 +0530
"dd-MM-yyyy"
};
@Override
public Date deserialize(JsonElement jsonElement, Type typeOF,
JsonDeserializationContext context) throws JsonParseException {
for (String format : DATE_FORMATS) {
try {
return new SimpleDateFormat(format, Locale.US).parse(jsonElement.getAsString());
} catch (ParseException e) {
}
}
throw new JsonParseException("Unparseable date: \"" + jsonElement.getAsString()
+ "\". Supported formats: " + Arrays.toString(DATE_FORMATS));
}
}
一切设置正确后,它会抛出错误消息
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 183 path $[0].horoscope[0].horoscope
,请告诉我我的代码中是否有任何错误。提前致谢。 最佳答案
错误消息是很不言自明的。$[0].horoscope[0].horoscope
是String
,但是您正在尝试将其反序列化为List
。
这是因为在HoroscopeModel
中,您将horoscope
定义为List<HoroscopeModel>
的类型,而不是List<Horoscope>
。
Gson试图将horoscope
(a String
)的值反序列化为horoscope
(a HoroscopeModel
)的List
字段,而不是horoscope
(a Horoscope
)的String
字段。
在HoroscopeModel
中,更改为以下字段:
private List<HoroscopeModel> horoscope;
对此:
private List<Horoscope> horoscope;
(也不要忘记修改相应的方法)