gpt4 book ai didi

java - HttpResponse 响应字符串有额外的 `"` 并且无法识别 `\n`

转载 作者:行者123 更新时间:2023-12-02 05:24:02 25 4
gpt4 key购买 nike

我有一个实现 Route 接口(interface)(spark 包)的类,该类应该在 handle 函数中返回一个字符串。当我将响应转换为字符串时,我的字符串的开头和结尾有两个额外的 " ,并且 \n 在我的字符串中不被识别为 1 个字符,而是如2.有人知道修复方法吗?

这是代码,responseHttpResponse 的实例:

Assert.assertEquals(200, response.getStatusLine().getStatusCode());
String output = EntityUtils.toString(response.getEntity())

下一个断言失败

Assert.assertEquals(10, output.length());
Java.lang.AssertionError: 
Expected :10
Actual :13

根据System.out输出是

"123456789\n"

虽然它应该是 123456789 并在其末尾有 \n 但我不认为 \n 应该显示在System.out.print()

最佳答案

这可能会有所帮助,

HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseString);

引用-How can I get an http response body as a string in Java?

关于java - HttpResponse 响应字符串有额外的 `"` 并且无法识别 `\n`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56244155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com