我正在使用泽西客户端向网络服务提出请求。
Client client = ClientBuilder.newClient(new ClientConfig());
Invocation.Builder builder = client.target("http://someurl.com").request();
String result = builder.get(String.class);
然后我收到了回复
<?xml version="1.0" encoding="ISO-8859-1" ?>
<DATA>some data with é è à characters</DATA>
但是在我的String结果中,响应看起来像那样
<?xml version="1.0" encoding="ISO-8859-1" ?>
<DATA>some data with � � � characters</DATA>
如何告诉球衣正确解码网络服务响应?
答案 0 :(得分:4)
感谢Wizbot,我今天遇到了完全相同的问题。
我想在没有Guava依赖的情况下发布我的java 8解决方案:
Client client = ClientBuilder.newClient(new ClientConfig());
Invocation.Builder builder = client.target("http://someurl.com").request();
Response response = builder.get();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader((InputStream) response.getEntity(), StandardCharsets.ISO_8859_1));
String result = bufferedReader.lines().collect(Collectors.joining(""));
答案 1 :(得分:1)
我确实找到了解决方法
Client client = ClientBuilder.newClient(new ClientConfig());
Invocation.Builder builder = client.target("http://someurl.com").request();
Response response = builder.get();
String result = CharStreams.toString(new InputStreamReader((InputStream) response.getEntity(), Charsets.ISO_8859_1));
CharStreams
是一个Guava类,但还有其他方法可以将InputStream
转换为String
并使用右Charset
。