美文网首页
Retrofit 泛型解析遇到com.google.gson.i

Retrofit 泛型解析遇到com.google.gson.i

作者: 云烟渐成雨 | 来源:发表于2019-08-02 18:27 被阅读0次

    在使用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;
            }
    
        }
    }
    

    相关文章

      网友评论

          本文标题:Retrofit 泛型解析遇到com.google.gson.i

          本文链接:https://www.haomeiwen.com/subject/mddrdctx.html