【问题标题】:retrofit2. response body is null改造2。响应正文为空
【发布时间】:2016-06-27 02:24:56
【问题描述】:
    Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.Q_BASE)
            .addConverterFactory(GsonConverterFactory.create()).client(client).build();
    Api.ApiInter inter = retrofit.create(Api.ApiInter.class);
    Call<JSONObject> call = inter.getMovieData(sortType[0], Api.P_KEY);
    call.enqueue(new Callback<JSONObject>() {
        @Override
        public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
            /* */
        }

        @Override
        public void onFailure(Call<JSONObject> call, Throwable t) {
            Log.d(LOG_TAG, "failed to get response.");
        }
    });

/

public class Api {
    public static final String Q_BASE = "http://api.themoviedb.org/";
    public static final String Q_KEY = "api_key";
    public static final String Q_RESULT = "results";
    public static final String Q_POSTER_PATH = "poster_path";
    public static final String Q_TITLE = "title";
    public static final String Q_OVERVIEW = "overview";
    public static final String Q_DATE = "release_date";
    public static final String Q_VOTE_AVERAGE = "vote_average";
    public static final String P_KEY = "This is private key";
    public static final String REQUEST = "GET";

    public static final String I_BASE = "http://image.tmdb.org/";
    public static final String P_POSTER_SIZE = "w185";

    public interface ApiInter {
        @GET("3/movie/{sort}")
        Call<JSONObject> getMovieData(@Path("sort") String sort, @Query("api_key") String key);

        @GET("t/p/{size}/{url}")
        Call<Bitmap> getMoviePoster(@Path("size") String size, @Path("url") String url);
    }
}

[

当我调试我的应用程序时,response.body() 总是只返回“{}”。如果我更改 Call 的元素,返回值将为空。我不知道原因。 我想获取 JSON 数据。我该如何解决? 我可以设置 okhttp 客户端吗?或更改呼叫的返回类型? 请给我答案。

【问题讨论】:

  • 后弯好像没有数据

标签: android json network-programming retrofit2


【解决方案1】:

这是一个非常简单的错误。 如果你有像我一样的问题,请检查这 3 件事。

  1. 您的改装转换器是 Gson。
  2. 你的api接口方法返回Call&lt;JSONObjcet&gt;

  3. 请求成功但 response.body() 只是“{}”

改造使用 GsonConverter。所以我们需要使用包com.google.gson中的jsonObject。 您必须将JSONObject 更改为JsonObject

【讨论】:

  • 你能说得详细点吗?
【解决方案2】:

图片中的响应码为200,表示请求成功。所以服务器正在返回 {}。也许没有与您作为参数传递的 sortType 相关的数据。

【讨论】:

  • 我检查了请求的url,但没有错。我认为问题不在于服务器或网址。
猜你喜欢
  • 2016-02-18
  • 2018-02-16
  • 2016-03-12
  • 2020-09-26
  • 2017-08-07
  • 2017-04-19
  • 1970-01-01
  • 1970-01-01
  • 2018-05-16
相关资源
最近更新 更多