Jrog
Jrog

Reputation: 1527

retrofit2. response body is null

    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);
    }
}

[Debugger]

When I debugged my app, response.body() always retruned only "{}". And if I change Call's element, return values will be null. I don't know the reason. I want to get JSON data. How I fix that? May I set okhttp client? or change Call's return type? Please give me the answer.

Upvotes: 3

Views: 8679

Answers (2)

Jrog
Jrog

Reputation: 1527

It is a very simple mistake. If you have a problem like me, check this 3 things.

  1. Your retrofit converter is Gson.
  2. Your api interface method returns Call<JSONObjcet>

  3. request is successful but response.body() is just "{}"

retrofit uses GsonConverter. So we need to use jsonObject in package com.google.gson. You must change JSONObject to JsonObject

Upvotes: 11

user3215142
user3215142

Reputation: 326

Response code is 200 in the image that means the request was successful. So server is returning {}. Maybe there is no data related to sortType that you are passing as parameter.

Upvotes: 1

Related Questions