java - 在 android 中调用 API 并使用 Retrofit 2 收到此错误 :Response{protocol=h2, code=200, message=, url=https ://api. com/video/list-video.php}

标签 java android api retrofit2 response

log response我得到的值是空响应。

我的 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/

相关文章:

java - 将 iOS 应用程序转换为 Android 应用程序

android - 无法调度 DDM block 52454151 : no handler defined error

java - 谷歌将 api 坐标从 java 映射到 javascript

java - HtmlUnit 无法登录表单提交后返回相同的页面

java - Android Studio 3.0 金丝雀 8 : Advanced profiling is unavailable for the selected process

java - 将变量传递给 Java (android) 中的嵌套方法并返回它

Android:为 "Move to SD Card"选项升级 API

sharepoint - 以编程方式设置SharePoint页面的标题?

java - 转换 String[] 参数。在java中int/double?

java - 如何管理扩展另一个类型的集合?