masoud vali masoud vali - 2 months ago 27
JSON Question

Unable to get retrofit v2 call response

I'm upgrading retrofit 1.9 to v2.1.0. I want to get the response json and convert it manually but I got something like this:

log res retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@41acd528-okhttp3.ResponseBody$1@41ad8a18


my code is :

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);

client = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.addInterceptor(logging)
.build();

Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(Config.baseEndpoint)
.build();

ApiService api = retrofit.create(ApiService.class);

Call<ResponseBody> login = api.login("email", "password");

login.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.e(TAG, "log res "+call.toString()+"-"+response.body().toString());
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {

}
});

Answer

use JsonObject instead of ResponseBody

Call<JsonObject> login = api.login("email", "password");

login.enqueue(new Callback<JsonObject>() {
    @Override
    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
        Log.e(TAG, "log res "+call.toString()+"-"+response.body().toString());
    }

    @Override
    public void onFailure(Call<JsonObject> call, Throwable t) {

    }
});

Note : make sure to use com.google.gson.JsonObject, not org.json.JSONObject