I\'m doing my API REST and I saw that in a lot of examples people use EntityUtils.toString(response)
to get their response
into a String
Beware the default charset of EntityUtils.toString
:
Get the entity content as a String, using the provided default character set if none is found in the entity. If defaultCharset is null, the default "ISO-8859-1" is used.
org.apache.http.util.EntityUtils.toString(response.getEntity, "UTF-8");
An HttpEntity
represents the content of the body of an HTTP response. EntityUtils.toString(HttpEntity)
interprets that content as a String
and returns it to you.
If your HTTP response is something like this
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: 80
<?xml version="1.0" encoding="utf-8"?>
<root>
<nested attr="whatever" />
</root>
Then the String
returned by EntityUtils.toString(HttpEntity)
will simply contain
<?xml version="1.0" encoding="utf-8"?>
<root>
<nested attr="whatever" />
</root>