首页 > 解决方案 > java.lang.IllegalStateException:应为 BEGIN_OBJECT,但在第 3 行第 1 列路径 $

问题描述

我正在使用带有 Gson 转换器的 retrofit2。

我的服务器响应是:

     {
    "resonse": {
        "status": 200,
        "result": [
            {
                "video_id": "3c19979979",
                "video_title": "Sushil Kumar Modi press conference after serial bomb blasts at Modi rally in Patna",
                "video_description": "BJP at Patna serial blast in Bihar, Nitish government has stood in the dock. Former Deputy Chief Minister Sushil Kumar Modi said the blasts Narendra Modi were targeted. He said that Nitish Kumar look Modi as the enemy.<br />\r\n",
                "video_poster": "https://vbcdn.com/cdn/download/2013102913830306761810268995.jpg",
                "video_duration": "02:02",
                "video_category": "News/Politics"
    }
  ]}
}

这是我的 pojo 课:

public class ResponseVideoList implements  Serializable {

    @SerializedName("resonse")
    @Expose
    private Resonse resonse;
    private final static long serialVersionUID = -2645239251698186770L;

    public Resonse getResonse() {
        return resonse;
    }

    public void setResonse(Resonse resonse) {
        this.resonse = resonse;
    }

    public class Resonse implements Serializable {

        @SerializedName("status")
        @Expose
        private Integer status;
        @SerializedName("result")
        @Expose
        private List<VIdeoItem> result = null;
        private final static long serialVersionUID = 3604737417113897610L;

        public Integer getStatus() {
            return status;
        }

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

        public List<VIdeoItem> getResult() {
            return result;
        }

        public void setResult(List<VIdeoItem> result) {
            this.result = result;
        }
    }

}

视频项目类:

 public class VIdeoItem implements Serializable
{

@SerializedName("video_id")
@Expose
private String videoId;
@SerializedName("video_title")
@Expose
private String videoTitle;
@SerializedName("video_description")
@Expose
private String videoDescription;
@SerializedName("video_poster")
@Expose
private String videoPoster;
@SerializedName("video_duration")
@Expose
private String videoDuration;
@SerializedName("video_category")
@Expose
private String videoCategory;
private final static long serialVersionUID = -7124444558733051869L;

public String getVideoId() {
return videoId;
}

public void setVideoId(String videoId) {
this.videoId = videoId;
}

public String getVideoTitle() {
return videoTitle;
}

public void setVideoTitle(String videoTitle) {
this.videoTitle = videoTitle;
}

public String getVideoDescription() {
return videoDescription;
}

public void setVideoDescription(String videoDescription) {
this.videoDescription = videoDescription;
}

public String getVideoPoster() {
return videoPoster;
}

public void setVideoPoster(String videoPoster) {
this.videoPoster = videoPoster;
}

public String getVideoDuration() {
return videoDuration;
}

public void setVideoDuration(String videoDuration) {
this.videoDuration = videoDuration;
}

public String getVideoCategory() {
return videoCategory;
}

public void setVideoCategory(String videoCategory) {
this.videoCategory = videoCategory;
}

}

我认为没有正确获取响应,我也尝试过类似的其他答案,但没有针对这个特定问题得到任何适当的解决方案。第 3 行是 "status": 200, this one 但无法找出解决方案。

改造客户:

 OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request newRequest  = chain.request().newBuilder()
                        .addHeader("Authorization", "Bearer " + token)
                        .build();
                return chain.proceed(newRequest);
            }
        }).build();
       if (retrofit==null)
       retrofit = new Retrofit.Builder()
                .baseUrl(API_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
               .client(client)
               .build();
        return retrofit;

API客户端:

  @Headers("Content-Type:application/json")
    @POST("video/list-video.php")
    Call<ResponseVideoList> getVideoListFromSearchText(@Body JsonObject jsonObject);

日志:

2019-08-14 10:21:19.274 6777-8289/com.veblr.android.veblrapp D/OkHttp: --> POST https://api.com/api/veblr-app/veblrAppNews/video/list-video .php 2019-08-14 10:21:19.274 6777-8289/com.veblr.android.veblrapp D/OkHttp:内容类型:应用程序/json 2019-08-14 10:21:19.275 6777-8289/com。 veblr.android.veblrapp D/OkHttp: Content-Length: 45 2019-08-14 10:21:19.276 6777-8289/com.veblr.android.veblrapp D/OkHttp: {"param":{"max_results":" 1","search":"modi"}} 2019-08-14
10:21:19.276 6777-8289/com.veblr.android.veblrapp D/OkHttp: --> END POST (45-byte body) 2019- 08-14 10:21:20.452 6777-8264/com.veblr.android.veblrapp V/FA:不活动,与服务断开连接 2019-08-14 10:21:20.466 6777-8289/com.veblr.android。 veblrapp D/OkHttp: <-- 200https://api.com/api/veblr-app/veblrAppNews/video/list-video.php (1189ms) 2019-08-14 10:21:20.466 6777-8289/com.veblr.android.veblrapp D/OkHttp : 服务器: nginx 2019-08-14 10:21:20.466 6777-8289/com.veblr.android.veblrapp D/OkHttp: 日期: Wed, 14 Aug 2019
04:51:23 GMT 2019-08-14 10:21 :20.466 6777-8289/com.veblr.android.veblrapp D/OkHttp: 内容类型: application/json 2019-08-14 10:21:20.466 6777-8289/com.veblr.android.veblrapp D/OkHttp: 不同: 接受编码
2019-08-14 10:21:20.467 6777-8289/com.veblr.android.veblrapp D/OkHttp: access-control-allow-origin: * 2019-08-14 10:21:20.467 6777- 8289/com.veblr.android.veblrapp D/OkHttp:严格传输安全:max-age=15768000 2019-08-14 10:21:20.472 6777-8289/com.veblr.android.veblrapp D/OkHttp:
2019 -08-14
10:21:20.472 6777-8289/com.veblr.android.veblrapp D/OkHttp: 警告:Redis::connect():connect() 失败: /home/veblr/public_html/api/veblr-app/中的连接被拒绝第7 行的veblrAppNews/config/redis.php
2019-08-14 10:21:20.472 6777-8289/com.veblr.android.veblrapp D/OkHttp: { {"resonse":{"status":200,"result":[{"video_id":"3c19979979 ","video_title":"Sushil Kumar Modi 在巴特那举行的莫迪集会发生连环炸弹爆炸后的新闻发布会","video_description":"BJP 在比哈尔邦的巴特那连环爆炸中,尼提什政府已经站在了被告席上。前副首席部长 Sushil Kumar莫迪说爆炸是纳伦德拉·莫迪的目标。他说尼蒂什·库马尔将莫迪视为敌人。\r\n","video_poster":"https://vb.com/cdn/download/2013102913830306761810268995.jpg","video_duration ":"02:02","video_category":"新闻/政治"}]}} 2019-08-14 10:21:20.473 6777-8289/com.veblr.android.veblrapp D/OkHttp: <-- END HTTP (1461-byte body) 2019-08-14 10:21:20.480 6777-6777/com.veblr.android.veblrapp E/FAiled: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 3 column 1 条路径 $

标签: androidgsonretrofit2jsonresponse

解决方案


删除 ResponseVideoList 类中的所有内容,然后将其放入;之后在你的改造中使用这个类,完整的响应将在这个类中解析

public class ResponseVideoList  {


    /**
     * resonse : {"status":200,"result":[{"video_id":"3c19979979","video_title":"Sushil Kumar Modi press conference after serial bomb blasts at Modi rally in Patna","video_description":"BJP at Patna serial blast in Bihar, Nitish government has stood in the dock. Former Deputy Chief Minister Sushil Kumar Modi said the blasts Narendra Modi were targeted. He said that Nitish Kumar look Modi as the enemy.<br />\r\n","video_poster":"https://vbcdn.com/cdn/download/2013102913830306761810268995.jpg","video_duration":"02:02","video_category":"News/Politics"}]}
     */

    private ResonseBean resonse;

    public ResonseBean getResonse() {
        return resonse;
    }

    public void setResonse(ResonseBean resonse) {
        this.resonse = resonse;
    }

    public static class ResonseBean {
        /**
         * status : 200
         * result : [{"video_id":"3c19979979","video_title":"Sushil Kumar Modi press conference after serial bomb blasts at Modi rally in Patna","video_description":"BJP at Patna serial blast in Bihar, Nitish government has stood in the dock. Former Deputy Chief Minister Sushil Kumar Modi said the blasts Narendra Modi were targeted. He said that Nitish Kumar look Modi as the enemy.<br />\r\n","video_poster":"https://vbcdn.com/cdn/download/2013102913830306761810268995.jpg","video_duration":"02:02","video_category":"News/Politics"}]
         */

        private int status;
        private List<ResultBean> result;

        public int getStatus() {
            return status;
        }

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

        public List<ResultBean> getResult() {
            return result;
        }

        public void setResult(List<ResultBean> result) {
            this.result = result;
        }

        public static class ResultBean {
            /**
             * video_id : 3c19979979
             * video_title : Sushil Kumar Modi press conference after serial bomb blasts at Modi rally in Patna
             * video_description : BJP at Patna serial blast in Bihar, Nitish government has stood in the dock. Former Deputy Chief Minister Sushil Kumar Modi said the blasts Narendra Modi were targeted. He said that Nitish Kumar look Modi as the enemy.<br />
             * video_poster : https://vbcdn.com/cdn/download/2013102913830306761810268995.jpg
             * video_duration : 02:02
             * video_category : News/Politics
             */

            private String video_id;
            private String video_title;
            private String video_description;
            private String video_poster;
            private String video_duration;
            private String video_category;

            public String getVideo_id() {
                return video_id;
            }

            public void setVideo_id(String video_id) {
                this.video_id = video_id;
            }

            public String getVideo_title() {
                return video_title;
            }

            public void setVideo_title(String video_title) {
                this.video_title = video_title;
            }

            public String getVideo_description() {
                return video_description;
            }

            public void setVideo_description(String video_description) {
                this.video_description = video_description;
            }

            public String getVideo_poster() {
                return video_poster;
            }

            public void setVideo_poster(String video_poster) {
                this.video_poster = video_poster;
            }

            public String getVideo_duration() {
                return video_duration;
            }

            public void setVideo_duration(String video_duration) {
                this.video_duration = video_duration;
            }

            public String getVideo_category() {
                return video_category;
            }

            public void setVideo_category(String video_category) {
                this.video_category = video_category;
            }
        }
    }
}

推荐阅读