Retrofit: Empty JSONObject even though logging was OK

Created on 4 Nov 2015  路  2Comments  路  Source: square/retrofit

Hi everyone,

compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.okhttp:logging-interceptor:2.6.0-SNAPSHOT'

Here is my Retrofit initialization (enabling OkHttp logging)

OkHttpClient client = new OkHttpClient();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
client.interceptors().add(interceptor);

// Initialize Retrofit
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://myendpoint.com")
        .client(client)
        .addConverterFactory(GsonConverterFactory.create())
        .build();

After executing this command of my API (which works fine on Postman)

@GET("data")
Call<JSONObject> getData();

The Response<JSONObject> reponse has an empty JSONObject on its response.body, which makes no sense even though OkHttp logging Interceptor was able to print the body data correctly.

Most helpful comment

The JSONObject type is from org.json.* not Gson. You want JsonObject from com.google.gson.*, or to bind to your own model class.

All 2 comments

The JSONObject type is from org.json.* not Gson. You want JsonObject from com.google.gson.*, or to bind to your own model class.

@JakeWharton Thank you very much :D

Was this page helpful?
0 / 5 - 0 ratings