EntityUtils.toString(response) 究竟返回什么?

Fra*_*ero 4 apache rest android entity

我做我的REST API,我看到了很多的例子人用EntityUtils.toString(response)得到他们response进入String他们的GETPOSTPUTDELETE方法。与此类似:

HttpGet method = new HttpGet(url);
method.setHeader("content-type", "application/json");
HttpResponse response = httpClient.execute(method);
String responseString = EntityUtils.toString(response.getEntity());
Run Code Online (Sandbox Code Playgroud)

请避免对我说它给我一个字符串的答案

我知道它返回给我一个String实体的内容(在这种情况下是响应),但这是我怀疑的地方,因为我对String返回的内容不放心。我在官方文档中看到了。

读取实体的内容并将其作为字符串返回。

https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/util/EntityUtils.html#toString(org.apache.http.HttpEntity,java.lang.String)

它是content-type什么在返回String

相反,我通过我的方法获得的值是在String?

我认为这是第二个问题,但我对此并不安全。而且,在这种情况下,这些值如何存储到String? 它们是由逗号或某些特殊字符分隔的吗?

提前致谢!

Sot*_*lis 5

AnHttpEntity表示 HTTP 响应正文的内容。EntityUtils.toString(HttpEntity)将该内容解释为 aString并将其返回给您。

如果您的 HTTP 响应是这样的

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>
Run Code Online (Sandbox Code Playgroud)

然后String返回的EntityUtils.toString(HttpEntity)将简单地包含

<?xml version="1.0" encoding="utf-8"?>
<root>
    <nested attr="whatever" />
</root>
Run Code Online (Sandbox Code Playgroud)