在 android 中调用 API 并使用 Retrofit 2 收到此错误:

http://img2.mukewang.com/646dc0210001d82a12070462.jpg

我得到空响应代替值。


我的 json 响应是


{

    "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",

    }

  ]}

}

改造客户端:


 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 时,状态码为 200,但响应主体始终为空。我收到的消息是这样的:


响应{protocol=h2, code=200, message=, url= https://api.com/video/list-video.php }


九州编程
浏览 155回答 3
3回答

紫衣仙女

似乎返回的 JSON 格式不正确(除非您裁剪文件以将其粘贴到此处...)我使用JSONLint来验证它

qq_遁去的一_1

仅适用于出现此错误的新人。当您的数据类(模型)与您正在使用的 json 不对应时,也会发生这种情况。您可以通过将 onResponse() 方法从 Retrofit 设置为 Any (kotlin) 或对象 (java) 来验证这一点&nbsp; &nbsp;@Override&nbsp; &nbsp; public void onResponse(Call<Object> call, Response<Object> response) {&nbsp; &nbsp; &nbsp; &nbsp; Log.d("zzz", "debug this line and check if response have ur object");&nbsp; &nbsp; }

慕雪6442864

乍一看,您的 json 似乎有误,在“video_category”之后还有一个“,”:“新闻/政治”,<--- 也许这就是问题所在Btw i would do my pojo like this&nbsp;我用这个网站自动创建它http://www.jsonschema2pojo.org/你总是可以使用这个网站来验证你的 JSON https://jsonformatter.curiousconcept.com/-----------------------------------ResponseVideoList.java-----------------------------------package com.example;import com.google.gson.annotations.Expose;import com.google.gson.annotations.SerializedName;public class ResponseVideoList{@SerializedName("resonse")@Exposeprivate Resonse resonse;public Resonse getResonse() {return resonse;}public void setResonse(Resonse resonse) {this.resonse = resonse;}}-----------------------------------Resonse.java-----------------------------------package com.example;import java.util.List;import com.google.gson.annotations.Expose;import com.google.gson.annotations.SerializedName;public class Resonse {@SerializedName("status")@Exposeprivate Integer status;@SerializedName("result")@Exposeprivate List<Result> result = null;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;}}-----------------------------------Result.java-----------------------------------package com.example;import com.google.gson.annotations.Expose;import com.google.gson.annotations.SerializedName;public class Result {@SerializedName("video_id")@Exposeprivate String videoId;@SerializedName("video_title")@Exposeprivate String videoTitle;@SerializedName("video_description")@Exposeprivate String videoDescription;@SerializedName("video_poster")@Exposeprivate String videoPoster;@SerializedName("video_duration")@Exposeprivate String videoDuration;@SerializedName("video_category")@Exposeprivate String videoCategory;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;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java