我正在尝试使用非拉丁字符集为 Android 和 iOS 发送通知。
我注意到当我使用非拉丁字符集从 Android 向 iOS 发送消息时,消息在 iPhone 上显示为“????”,因为 iOS 和 Android 的 Java 服务器端是相同的,我认为问题是我如何发送来自 Android 手机的请求,从 iOS 到 iOS 的通知消息工作正常。
下面是我用来打开网络连接和发送请求的代码,请告诉我是否可以。
byte[] bytes = body.getBytes(/*"UTF-16"*//*"ISO-8859-1"*/"UTF-8");
HttpURLConnection conn = null;
StringBuilder stringBuilder = new StringBuilder();
try {
conn = (HttpURLConnection) url.openConnection();//conn.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setFixedLengthStreamingMode(bytes.length);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
// post the request
OutputStream out = conn.getOutputStream();
out.write(bytes);
out.close();
// handle the response
int status = conn.getResponseCode();
if (status != 200) {
Log.d("send message", "Coud Send Message, No Internet Connection Avilable.");
throw new IOException("Post failed with error code " + status);
}
InputStream in = new BufferedInputStream(conn.getInputStream());
// readStream(in);
int b;
while ((b = in.read()) != -1) {
stringBuilder.append((char) b);
}