我想使用java API发送包含阿拉伯语消息的短信
mobily provider提供了一个java api来发送短信
我使用了这段代码:
public void sendMessage(String userName,String password,String sender,String message,String numbers){
String para ="mobile=" + userName + "&password=" + password + "&numbers=" + numbers+ "&sender=" + sender + "&msg=" + convertUnicode(message) + "&applicationType=24";
sendURL("http://www.mobily.ws/api/msgSend.php",para,1);
System.out.println(getMessage());
}
public static String convertUnicode(String a) {
int bufSize = 16;
byte[] buffer = new byte[bufSize];
String s = null;
try {
buffer=a.getBytes();
s = bytesToHex(buffer,0,buffer.length);
System.out.println("Hex: "+s);
} catch (Exception e) {
System.out.println(e.toString());
}
return s;
}
public static String bytesToHex(byte[] b, int off, int len) {
StringBuffer buf = new StringBuffer();
for (int j=0; j<len; j++)
buf.append(byteToHex(b[off+j]));
return buf.toString();
}
public static String byteToHex(byte b) {
char[] a = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] };
return forDigits(new String(a));
}
public static String forDigits(String val){
switch (val.length() ){
case 1:return "000"+val;
case 2:return "00"+val;
case 3:return "0"+val;
case 4:return ""+val;
default:return val;
}
}
public void sendURL(String URL,String parameters,int operationNumber){
try {
URL url;
URLConnection urlConnection;
DataOutputStream outStream;
// Create connection
url = new URL(URL);
urlConnection = url.openConnection();
((HttpURLConnection)urlConnection).setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Content-Length", ""+ parameters.length());
urlConnection.setRequestProperty("User-agent","Mozilla/4.0");
// Create I/O streams
outStream = new DataOutputStream(urlConnection.getOutputStream());
// Send request
outStream.writeBytes(parameters);
outStream.flush();
outStream.close();
// Get Response
BufferedReader rd = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
// - For debugging purposes only!
String buffer;
while((buffer = rd.readLine()) != null) {
try{
selectedMessage(Integer.parseInt(buffer),operationNumber);
}catch(Exception ex){
balance=buffer;
}
}
// Close I/O streams
rd.close();
outStream.close();
}
catch(Exception ex) {
System.out.println("Exception cought:\n"+ ex.toString());
}
}
问题是缓冲区值是“-1 - ”
这一行填写在这一行:
buffer = rd.readLine()
所以我总是发现自己处于这个例外
}catch(Exception ex){ balance=buffer; }
sendMessage 方法中发送的参数:
发件人:شارععلي
消息:وجهتإلى号码:00966569114455
已更新:
我到了发送英文信息
发件人:测试
消息:测试
在此行while((buffer = rd.readLine()) != null) {
缓冲区的值等于 1
问题仅适用于发送阿拉伯语消息
我尝试使用以下方法更改代码但未成功:
while((buffer = rd.readLine()) != null) {
try{
buffer = buffer.replaceAll("(-?[0-9]+)([^0-9]*)?","$1");
buffer=buffer.replace("-", "");
selectedMessage(Integer.parseInt(buffer),operationNumber);
}catch(Exception ex){
balance=buffer;
}
也在 sendURL 方法的这一行:
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
并在 convertUnicode 方法的这一行:
buffer=a.getBytes(StandardCharsets.UTF_8);
这是我的函数,它返回最后的消息:
public void selectedMessage(int value,int operationNumber){
switch(operationNumber){
case 1:switch(value){
case 1:msg= "SUCCESS";break;
case 2:msg="ERROR";break;
}break;
}
}
使用我修改过的代码我强制缓冲区值等于1
sendURL方法参数的值为:
mobile=966556541236&password=123654&numbers=966569114455&sender=شارع علي&msg=00D800A700D9008400D9008500D800B900D800A700D9008500D9008400D800A9002000D800B100D9008200D9008500D9008800D800AC00D9008700D800AA002000D800A500D9008400D90089002000D9008600D800B800D800A700D90085002000D9008400D9008400D800AA00D800AF00D800B100D9008A00D800A8&applicationType=24
答案 0 :(得分:0)
您可以考虑先在输入上执行RegEx replaceAll,以过滤输入。
buffer = rd.readLine().replaceAll("(-?[0-9]+)([^0-9]*)","$1");
这将转换输入如下:
-1- -> -1
-1 -> -1
1- -> 1