OkHttpClient的坑

  • Post author:
  • Post category:其他


一、在返回值中拿不到返回的数据。

OkHttpClient没有返回Json数据。

client.newCall(request).enqueue(new okhttp3.Callback() {
    @Override
    public void onFailure(okhttp3.Call call, IOException e) {
        Log.d("ActivityOkHttpClient","ActivityOkHttpClient onResponse");
    }

    @Override
    public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
        Log.d("ActivityOkHttpClient","ActivityOkHttpClient onResponse"+ new Gson().toJson(response.body().string()));
        Log.d("ActivityOkHttpClient","ActivityOkHttpClient onResponse"+ new Gson().toJson(response.message()));

        //回调方法在子线程中,更新UI需要用handler
       // retrofitTv.setText(new Gson().toJson(response.body()));
    }
});

(1)开始 response.body().toString(); 一直拿不到json数据。

(2)替换为response.body().string()即可



版权声明:本文为u011638706原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。