我的 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",
}
]}
}
retrofit 客户端:
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);
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 String status;
@SerializedName("result")
@Expose
private List<VIdeoItem> result = null;
private final static long serialVersionUID = -350882274147346830L;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<VIdeoItem> getResult() {
return result;
}
public void setResult(List<VIdeoItem> result) {
this.result = result;
}
}
}
每当我调用 api 时,状态码为 200,但响应主体始终为空。 我收到的消息是这样的:
响应{协议(protocol)=h2,代码=200,消息=,url= https://api.com/video/list-video.php
如有任何帮助或建议,我们将不胜感激。
最佳答案
仅针对遇到此错误的新手。当您的数据类(模型)与您使用的 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");
}
关于java - 在 android 中调用 API 并使用 Retrofit 2 收到此错误 :Response{protocol=h2, code=200, message=, url=https ://api. com/video/list-video.php},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57428312/