亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 android 中調用 API 并使用 Retrofit 2 收到此錯誤:

在 android 中調用 API 并使用 Retrofit 2 收到此錯誤:

九州編程 2023-05-24 15:42:59
我得到空響應代替值。我的 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 }
查看完整描述

3 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

似乎返回的 JSON 格式不正確(除非您裁剪文件以將其粘貼到此處...)

http://img3.sycdn.imooc.com/646dc03f00012e5e10920462.jpg

我使用JSONLint來驗證它



查看完整回答
反對 回復 2023-05-24
?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

僅適用于出現此錯誤的新人。當您的數據類(模型)與您正在使用的 json 不對應時,也會發生這種情況。


您可以通過將 onResponse() 方法從 Retrofit 設置為 Any (kotlin) 或對象 (java) 來驗證這一點


   @Override

    public void onResponse(Call<Object> call, Response<Object> response) {

        Log.d("zzz", "debug this line and check if response have ur object");

    }


查看完整回答
反對 回復 2023-05-24
?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

乍一看,您的 json 似乎有誤,在“video_category”之后還有一個“,”:“新聞/政治”,<--- 也許這就是問題所在


Btw i would do my pojo like this 

我用這個網站自動創建它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")

@Expose

private 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")

@Expose

private Integer status;

@SerializedName("result")

@Expose

private 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")

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


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;

}


}



查看完整回答
反對 回復 2023-05-24
  • 3 回答
  • 0 關注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號