java.lang.IllegalStateException:预期为BEGIN_OBJECT,但位于第3行第1列路径$

时间:2019-08-13 06:34:02

标签: android gson retrofit2 jsonresponse

我正在将Retrofit2与Gson Converter一起使用。

我的服务器响应是:

     {
    "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;
        }
    }

}

VideoItem类:

 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行是“状态”:200,但无法找出解决方案。

改装客户端:

 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:内容类型:application / json 2019-08-14 10:21:19.275 6777-8289 / com.veblr.android.veblrapp D / OkHttp:内容长度: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字节正文) 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:<-200 https://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:日期:2019年8月14日,星期三
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:strict-transport-security: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 / veblrAppNews / config / redis.php 在线 7
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站在码头上。前副首席部长苏希尔·库马尔·莫迪说纳伦德拉·莫迪是爆炸的目标。他说,尼特·库马尔的样子\ 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:<-结束HTTP(1461字节主体)2019-08-14 10:21:20.480 6777-6777 / com.veblr.android.veblrapp E /失败:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但位于第3行第1列路径$

3 个答案:

答案 0 :(得分:0)

删除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;
            }
        }
    }
}

答案 1 :(得分:0)

public class Example implements Serializable
{

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

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<Result> result = null;
private final static long serialVersionUID = 3708794248294394638L;

public Integer getStatus() {
return status;
}

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

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

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

}




public class Result 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 = -7678430047668505370L;

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;
}

}

答案 2 :(得分:0)

为帮助您发现问题,请使用此库并查看网络日志和响应,然后您可以找到问题,

将此添加到dependencies

 implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'

现在将其添加到您的OkHttpClient

OkHttpClient client = new OkHttpClient.Builder()
       .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
       .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();

现在,在调用api之后,您可以在logcat中查看日志并查找问题