Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getResponseBody() should close Response, so use Response.body().string() #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

robinzt
Copy link

@robinzt robinzt commented Sep 2, 2021

TeaResponse.getResponseBody() should close okhttp's Response quietly, so use Response.body().string()

Such as alipay-easysdk (java) use TeaResponse.getResponseBody() to get body String, then parsed as json.
But it will be warned by OKHttp: "forget to close a response body"

2021-08-27 11:22:00.686 [OkHttp ConnectionPool] WARN okhttp3.OkHttpClient - 
A connection to https://openapi.alipaydev.com/ was leaked. Did you forget to close a response body? 
To see where this was allocated, set the OkHttpClient logger level to FINE: 
Logger.getLogger(OkHttpClient.class.getName()).setLevel(Level.FINE);

So use Response.body().string() to return body string, and it will be quietly closed Response.
Another benefit: it will handle charset correctly (first from content-type then UTF-8), the original using new String(bytes) but it will use system default charset and maybe not UTF-8.

Related issue:
alipay/alipay-easysdk#269

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@yndu13
Copy link
Contributor

yndu13 commented Sep 2, 2022

Hi. Thank you for your submission. Have you used this function? As far as I know, this function is not currently in use. We use the getResponse() in most cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants