OkHttpClient的坑

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

  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()即可

你可能感兴趣的:(安卓碰到的坑)