java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 3 column 1 path $

余生颓废 提交于 2019-12-31 07:53:09

问题


I am using retrofit2 with Gson Converter.

my server response is :

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

ANd this is my pojo class:

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 class:

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

}

the response is not fetched properly I think and I have also tried other answers like this but did not get any proper solution for this particular problem. the line 3 is "status": 200, this one but can't figure out the solution to this.

Retrofit client :

 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 CLIENT :

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

log:

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: Content-Type: application/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: Inactivity, disconnecting from the service 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: server: nginx 2019-08-14 10:21:20.466 6777-8289/com.veblr.android.veblrapp D/OkHttp: date: Wed, 14 Aug 2019
04:51:23 GMT 2019-08-14 10:21:20.466 6777-8289/com.veblr.android.veblrapp D/OkHttp: content-type: application/json 2019-08-14 10:21:20.466 6777-8289/com.veblr.android.veblrapp D/OkHttp: vary: Accept-Encoding
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: Warning: Redis::connect(): connect() failed: Connection refused in /home/veblr/public_html/api/veblr-app/veblrAppNews/config/redis.php on line 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 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.\r\n","video_poster":"https://vb.com/cdn/download/2013102913830306761810268995.jpg","video_duration":"02:02","video_category":"News/Politics"}]}} 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 path $


回答1:


Remove every thing in ResponseVideoList class and just Put this; after that use this one class in your retrofit complete response will parse in this class

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



回答2:


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

}



回答3:


for helping you and find your issue use this library and see network logs and your response, then you can find your problem,

Add this to dependencies

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

Now add it to your 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();

Now after call api you can see logs in logcat and finding your problem



来源:https://stackoverflow.com/questions/57472295/java-lang-illegalstateexception-expected-begin-object-but-was-string-at-line-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!