Reputation: 479
I use HttpUrlConnection to post outside a json but seems Chinese characters are changing to ????? I tried with different encoding style like utf-16,big 5 but I cant understand what is causing this. When I debug this, I can see chineese character before post, but when post, it changes why? code parts is in the below
String postData,String charset) throws MalformedURLException, IOException{
URL url = new URL(targetUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(
Proxy.NO_PROXY);
connection.setConnectTimeout(postTimeout);
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
contentType+"; charset="+charset);//+charset.getName().toUpperCase());//+charset.getName());
sleep(sleepTime);
OutputStream os = connection.getOutputStream();
//"UnicodeBigUnmarked"
//
// byte[] bt= postData.getBytes();
// System.out.println(bt);
// os.write(bt);
// System.out.println();
// os.flush();
//System.out.println(postData);
try
{
Writer writer = new OutputStreamWriter(os, charset);
writer.write(postData);
writer.flush();
writer.close();
} catch (IOException e) {
logger.severe("Http POST exception");
} finally {
if (os != null) {
os.close();
}
}
int responseCode = connection.getResponseCode();
connection.disconnect();
return responseCode;
I tried with big5,utf-16, but still no change.
Thanks.
Upvotes: 0
Views: 1407
Reputation: 47637
I believe that you should use the unicode ascii-safe representation in JSon like explained here
Upvotes: 1