Sinch 阿拉伯语消息
Sinch Arabic Message
我需要使用 Sinch API 发送带有阿拉伯语文本的短信。我的短信包含 - 英语、阿拉伯语和数字号码。
我试过的
- 我已经阅读了 Sinch 的文档,但他们没有提供任何
关于如何使用 UTF-16BE 和 header 的详细文档
发送此消息时进行编码。
- 我也联系了他们的支持团队,但他们都没有提供详细的解决方案。他们提到的任何内容都没有用。
下面是我试过的代码
String arabicFormattedMessage = "Some English Text \n\n"+"رجى الاتصال بالامتثال التجاري وحماية المستهلك (ككب) من دائرة التنمية الاقتصادية في 12345678 لمزيد من التفاصيل، إذا لم يتم الاتصال في غضون 5 أيام عمل.";
jsonObject.put("message", arabicFormattedMessage );
String messageBody = new String(jsonObject.toString().getBytes(),"UTF-16BE");
String contentTypeHeader = "application/json; charset=UTF-16BE";
headers.put("authorization", base64AuthHeader);
headers.put("content-type", contentTypeHeader);
headers.put("x-timestamp", timeStampHeader);
//headers.put("encoding", "UTF-16BE"); Tried with and without this, but still not working.
回复我已记录
Sinch responseCode: 400
Sinch Response: message object is invalid. toNumber object is invalid.
根据评论中的要求,以下是仅使用英文的工作示例:
JSONObject jsonObject = new JSONObject();
String formattedMessage = "some english message";
jsonObject.put("message", formattedMessage );
final String messageBody = jsonObject.toString();
final String base64AuthHeader = "basic" + " " + base64AppDetails;
String contentTypeHeader = "application/json; charset=UTF-8";
final String timeStampHeader = DateUtil.getFormattedTimeString(new Date());
headers.put("authorization", base64AuthHeader);
headers.put("content-type", contentTypeHeader);
headers.put("x-timestamp", timeStampHeader);
HTTP POST 请求
HttpClient.sendHttpPostRequest(url, messageBody, headers);
调用成功后,收到Sinch回复的消息id
问题在于,无论您的字符串的内部编码是什么(在 Java 中始终是 UTF-16),HttpClient.sendHttpPostRequest 都可能将其作为 UTF-8 发送。
由于 DefaultHttpClient 已弃用,我们将使用 HttpURLConnection 以正确的方式进行。您可能想查看 HttpURLConnection Documentation 关于身份验证应如何使用它。
String arabicFormattedMessage = "Some English Text \n\n"+"رجى الاتصال بالامتثال التجاري وحماية المستهلك (ككب) من دائرة التنمية الاقتصادية في 12345678 لمزيد من التفاصيل، إذا لم يتم الاتصال في غضون 5 أيام عمل.";
jsonObject.put("message", arabicFormattedMessage );
URL url = new URL ("http://...");
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
try {
urlConn.setDoInput (true);
urlConn.setDoOutput (true);
urlConn.setRequestProperty("Content-Type","application/json; charset=UTF-16");
urlConn.connect();
DataOutputStream out = new DataOutputStream(urlConn.getOutputStream ());
out.writeBytes(URLEncoder.encode(jsonObject.toString(),"UTF-16"));
out.flush ();
out.close ();
int responseCode = urlConnection.getResponseCode();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
} finally {
urlConnection.disconnect();
}
}
如果它不起作用,您还应该尝试 UTF-16BE,或不带字节顺序标记的 UTF-16LE。
我需要使用 Sinch API 发送带有阿拉伯语文本的短信。我的短信包含 - 英语、阿拉伯语和数字号码。
我试过的
- 我已经阅读了 Sinch 的文档,但他们没有提供任何 关于如何使用 UTF-16BE 和 header 的详细文档 发送此消息时进行编码。
- 我也联系了他们的支持团队,但他们都没有提供详细的解决方案。他们提到的任何内容都没有用。
下面是我试过的代码
String arabicFormattedMessage = "Some English Text \n\n"+"رجى الاتصال بالامتثال التجاري وحماية المستهلك (ككب) من دائرة التنمية الاقتصادية في 12345678 لمزيد من التفاصيل، إذا لم يتم الاتصال في غضون 5 أيام عمل.";
jsonObject.put("message", arabicFormattedMessage );
String messageBody = new String(jsonObject.toString().getBytes(),"UTF-16BE");
String contentTypeHeader = "application/json; charset=UTF-16BE";
headers.put("authorization", base64AuthHeader);
headers.put("content-type", contentTypeHeader);
headers.put("x-timestamp", timeStampHeader);
//headers.put("encoding", "UTF-16BE"); Tried with and without this, but still not working.
回复我已记录
Sinch responseCode: 400
Sinch Response: message object is invalid. toNumber object is invalid.
根据评论中的要求,以下是仅使用英文的工作示例:
JSONObject jsonObject = new JSONObject();
String formattedMessage = "some english message";
jsonObject.put("message", formattedMessage );
final String messageBody = jsonObject.toString();
final String base64AuthHeader = "basic" + " " + base64AppDetails;
String contentTypeHeader = "application/json; charset=UTF-8";
final String timeStampHeader = DateUtil.getFormattedTimeString(new Date());
headers.put("authorization", base64AuthHeader);
headers.put("content-type", contentTypeHeader);
headers.put("x-timestamp", timeStampHeader);
HTTP POST 请求
HttpClient.sendHttpPostRequest(url, messageBody, headers);
调用成功后,收到Sinch回复的消息id
问题在于,无论您的字符串的内部编码是什么(在 Java 中始终是 UTF-16),HttpClient.sendHttpPostRequest 都可能将其作为 UTF-8 发送。
由于 DefaultHttpClient 已弃用,我们将使用 HttpURLConnection 以正确的方式进行。您可能想查看 HttpURLConnection Documentation 关于身份验证应如何使用它。
String arabicFormattedMessage = "Some English Text \n\n"+"رجى الاتصال بالامتثال التجاري وحماية المستهلك (ككب) من دائرة التنمية الاقتصادية في 12345678 لمزيد من التفاصيل، إذا لم يتم الاتصال في غضون 5 أيام عمل.";
jsonObject.put("message", arabicFormattedMessage );
URL url = new URL ("http://...");
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
try {
urlConn.setDoInput (true);
urlConn.setDoOutput (true);
urlConn.setRequestProperty("Content-Type","application/json; charset=UTF-16");
urlConn.connect();
DataOutputStream out = new DataOutputStream(urlConn.getOutputStream ());
out.writeBytes(URLEncoder.encode(jsonObject.toString(),"UTF-16"));
out.flush ();
out.close ();
int responseCode = urlConnection.getResponseCode();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
} finally {
urlConnection.disconnect();
}
}
如果它不起作用,您还应该尝试 UTF-16BE,或不带字节顺序标记的 UTF-16LE。