okhttp安卓版没有返回正确的响应体

问题描述 投票:0回答:1

以下是我的代码中的okhttp部分

                    StringBuilder finalQuery = new StringBuilder(fq);
                    finalQuery.deleteCharAt(finalQuery.length()-1);
                    //System.out.println("Final search query : "+finalQuery.toString());

                    Request request = new Request.Builder().url(finalQuery.toString()).build();
                    client.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            System.out.println("FAILED!!!");
                            e.printStackTrace();
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            if(response.isSuccessful())
                            {
                                StringBuilder strResponse = new StringBuilder(response.body().toString());
                                System.out.println("strResponse: "+strResponse.toString());
                                resp=strResponse.toString();
                                Intent intent = new Intent(MainActivity.this, onResponse.class);
                                startActivity(intent);
                            }
                            else
                            {
                                System.out.println("NOT SUCCESSFUL");
                            }

                        }
                    });

字符串都不是空的。对网站的调用是成功的,但响应是体是这样的 。

2020-05-05 13:47:40.982 15129-15161/com.example.lyricsapp I/System.out: strResponse: okhttp3.internal.http.RealResponseBody@b650836

当我使用在线客户端使用相同的字符串检查响应时,它在那里很好。在我的代码中,它不是。我的代码中似乎有什么错误?

android android-studio okhttp
1个回答
0
投票

使用 string() 以字符串形式检索响应体内容。也就是说,将

response.body().toString()

response.body().string()

toString() 的方法,从 Object 而它只是给你类名和哈希码。

© www.soinside.com 2019 - 2024. All rights reserved.