我有一个3rd party api,它给了我一些json响应
{
"name": "บรีส",
"company": "Таиланд",
}
在上述Json回复中,我有一些泰语字符, 当我直接访问api时,我会得到正确的泰语字符,但是当我从RestClient调用它时,我会得到该字符为????
{
"name":"????????????",
"company":"??????????????"
}
这是我的RestClient代码
private void executeRequest(HttpUriRequest request, String url) throws CommonException, IOException{
//logger.debug(CLASS_NAME+" executeRequest : Entry :: url ->"+url + " Request: "+ postData );
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(connectionTimeOut)
.setConnectionRequestTimeout(soTimeOut)
.setSocketTimeout(soTimeOut)
.build();
CloseableHttpClient client;
client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig)
.useSystemProperties().build();
HttpContext localContext = new BasicHttpContext();
CloseableHttpResponse httpResponse = null ;
InputStream instream = null;
try {
httpResponse = client.execute(request, localContext);
responseCode = httpResponse.getStatusLine().getStatusCode();
message = httpResponse.getStatusLine().getReasonPhrase();
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
instream = entity.getContent();
response = convertStreamToString(instream);
}
} catch (ClientProtocolException e) {
//logger.debug(CLASS_NAME+" :: executeRequest : ClientProtocolException :: url ->"+url+ " responseCode -> " + responseCode + "message -> " + message + " Service Execution Rime: "+lSvcExecTime+"\n Request -> "+postData + " Exception : "+ e.getMessage() );
throw e;
}catch (SocketException e) {
//logger.debug(CLASS_NAME+" :: executeRequest : SocketException :: url ->"+url+ " responseCode -> " + responseCode + "message -> " + message + " Service Execution Rime: "+lSvcExecTime+"\n Request -> "+postData + " Exception : "+ e.getMessage() );
throw e;
} finally {
if(httpResponse!=null){
httpResponse.close();
}
if(null != instream){
instream.close();
}
}
}
private static String convertStreamToString(InputStream is) throws UnsupportedEncodingException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
try {
while((length = is.read(buffer)) != -1){
result.write(buffer,0,length);
}
} catch (IOException e) {
//logger.error(CLASS_NAME+" IOException due to - "+e.getMessage());
} finally {
try {
is.close();
} catch (IOException e) {
//logger.error(CLASS_NAME+"IOException inside finally due to - "+e.getMessage());
}
}
log.warning("Response in RestClient ---"+result.toString());
//return result.toString();
return new String(result.toString().getBytes(), "UTF-8");
}
此代码可以在我的本地系统中完美运行,但是当我在服务器上运行此代码时,它会给我????。
注意:调用RestClient时,我要添加标题-
client.AddHeader("Content-Type","application/json; charset=utf-8");
请指导我该怎么做。
谢谢。
答案 0 :(得分:1)
更改此表达式
new String(result.toString().getBytes(), "UTF-8")
到
new String(result.toByteArray(), "UTF-8")
首先将result
转换为字符串,然后再转换为字节,您将使用JVM的默认字符集转换数据,该默认字符集大概是本地计算机上的UTF-8,而不是服务器上的。 >
一些观察结果:
ByteArrayOutputStream
,然后再转换为String
:将InputStream
包裹在InputStreamReader
中(指定字符集),然后BufferedReader
:然后您可以直接阅读字符,并将其附加到StringBuilder
。