在使用Retrofit进行网络请求时出现了以下异常:
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.fr.konwledge.bean.TestBean
泛型解析错误。最初我的JavaBean类的写法如下:
public class TodayListBean {
private boolean isError;
private ResultsBean results;
private List<String> category;
public boolean isError() {
return isError;
}
public ResultsBean getResults() {
return results;
}
public List<String> getCategory() {
return category;
}
public static class ResultsBean<T> {
private List<T> Android;
private List<T> App;
private List<T> iOS;
public List<T> getAndroid() {
return Android;
}
public void setAndroid(List<T> android) {
Android = android;
}
public List<T> getApp() {
return App;
}
public void setApp(List<T> app) {
App = app;
}
public List<T> getiOS() {
return iOS;
}
public void setiOS(List<T> iOS) {
this.iOS = iOS;
}
}
}
后改成如下,解决了问题。
public class TodayListBean<T> {
private boolean isError;
private ResultsBean<T> results;
private List<String> category;
public boolean isError() {
return isError;
}
public ResultsBean<T> getResults() {
return results;
}
public List<String> getCategory() {
return category;
}
public static class ResultsBean<T> {
private List<T> Android;
private List<T> App;
private List<T> iOS;
public List<T> getAndroid() {
return Android;
}
public void setAndroid(List<T> android) {
Android = android;
}
public List<T> getApp() {
return App;
}
public void setApp(List<T> app) {
App = app;
}
public List<T> getiOS() {
return iOS;
}
public void setiOS(List<T> iOS) {
this.iOS = iOS;
}
}
}