加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 无法获取OkHttp的response.body.toString()来返回一个字

发布时间:2020-12-15 07:35:54 所属栏目:Java 来源:网络整理
导读:我正在尝试使用OkHttp获取一些json数据,并且无法弄清楚为什么当我尝试记录response.body().toString()时得到的结果是:results :: com.squareup.okhttp.Call$RealResponseBody@41c16aa8 try { URL url = new URL(BaseUrl); OkHttpClient client = new OkHttp
我正在尝试使用OkHttp获取一些json数据,并且无法弄清楚为什么当我尝试记录response.body().toString()时得到的结果是:results :: com.squareup.okhttp.Call$RealResponseBody@41c16aa8

try {
        URL url = new URL(BaseUrl);
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .header(/****/)
                .build();

        Call call = client.newCall(request);
        Response response = call.execute();

        **//for some reason this successfully prints out the response**
        System.out.println("YEAH: " + response.body().string());

        if(!response.isSuccessful()) {
            Log.i("Response code"," " + response.code());
        }

        Log.i("Response code",response.code() + " ");
        String results = response.body().toString();

        Log.i("OkHTTP Results: ",results);

我不知道我在这里做错了什么.我如何获得响应字符串?

解决方法

您已使用.string()函数在System.out.println()中打印响应.但最后在Log.i()中你使用.toString().

因此,请在响应正文上使用.string()进行打印并获取请求的响应,例如:

response.body().string();

注意:

> .toString():以字符串格式返回对象.
> .string():这将返回您的响应.

我认为这可以解决你的问题……对.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读