无法获得改造的v2呼叫响应

时间:2016-09-20 07:04:57

标签: android json callback retrofit2 okhttp3

我将改造1.9升级到v2.1.0。我想得到响应json并手动转换它但我有这样的东西:

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

我的代码是:

    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) {

        }
    });

1 个答案:

答案 0 :(得分:1)

使用JsonObject代替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) {

    }
});

注意:请务必使用com.google.gson.JsonObject,而不是org.json.JSONObject