1

in my application there is both arabic and english language suport but i am facing a problem when the mobile receive arabic SMS it is displaied as ??? ???? (question marks) knowing that the monbile i am using for testing supports arabic and all the arabic in the application is working fine the problem is only when an arabic SMS is received by my mobile.

String ff = new String(smsContent.getBytes("UTF-8"), "UTF-8");
            StringWriter stringBuffer = new StringWriter();
            PrintWriter pOut = new PrintWriter(stringBuffer);
            pOut.print("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        pOut.print("<!DOCTYPE MESSAGE SYSTEM \"http://127.0.0.1/psms/dtd/messagev12.dtd\" >");
        pOut.print("<MESSAGE VER=\"1.2\"><USER USERNAME=\""+userName+"\" PASSWORD=\""+password+"\"/>");
        pOut.print("<SMS UDH=\"0\" CODING=\"1\" TEXT=\""+ff+"\" PROPERTY=\"0\" ID=\"2\">");
        pOut.print("<ADDRESS FROM=\""+fromNo+"\" TO=\""+toNO+"\" SEQ=\"1\" TAG=\"\" />");
        pOut.print("</SMS>");
        pOut.print("</MESSAGE>");

        pOut.flush();
        pOut.close();


URL url = new URL("url");

        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.setDoOutput(true);

        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));

        out.write("data="+message+"&action=send");
        out.flush();

SMS in english working file in my application.

chetan
  • 2,995
  • 20
  • 67
  • 111
  • how do you send sms , code can help us – jmj Nov 15 '10 at 12:47
  • If you send UTF data to server try printing there to make sure that it has really reached there in UTF8 also , try debugging like this – jmj Nov 15 '10 at 12:58

3 Answers3

2

First, new String(smsContent.getBytes("UTF-8"), "UTF-8") is a redundant roundtrip, equivalent to smsContent. First you encode the string as bytes via UTF-8, and then immediately decode it back from the bytes again.

Second, your method of puzzling together XML is completely broken. You can't just concatenate strings and hope to end up with well-formed XML. Just for example think about what happens if someone tries to send a "? Use an XML library.

Third, you're implicitly using the platform default encoding for your OutputStreamWriter instead of explicitly specifying one, which means your code only works on those machines which randomly happen to have the correct encoding as default. I'm guessing yours does not.

Fourth, your method of puzzling together POST parameters is broken. You haven't specified what the variable message is. I'm guessing it's the complete XML document, but then you're trying to send it as a POST parameter to some kind of HTTP service, in which case it needs to be escaped/url-encoded. Just for example, what happens if someone tries to send the message &data=<whatever>&? Please clarify.

See also Using java.net.URLConnection to fire and handle HTTP requests

Fifth, since you're sending to some HTTP service, there's probably some documentation for that service what encoding to send or how to specify it, possibly with a HTTP header (Probably "Content-type: application/x-www-form-urlencoded; charset=UTF-8"?). Point us to the documentation if you can't figure it out yourself.

Edit: Found the documentation: http://www.google.se/search?q=valuefirst+pace

It pretty clearly states that you need to url encode the XML document, so that's probably what you're missing, in which case the encoding for the OutputStreamWriter won't matter as long as it's ASCII-compatible.

However, the documentation does not specify which character encoding to use for url-encoding, which is pretty weak. UTF-8 is the most likely though.

Community
  • 1
  • 1
Christoffer Hammarström
  • 24,236
  • 4
  • 44
  • 54
1

From what I've read on some internet pages, SMS in arabic languages (and others too) are encoded with UCS-2 and not UTF-8. Changing the encoding is worth a try.

Andreas Dolk
  • 108,221
  • 16
  • 168
  • 253
  • it will throws an exception -- java.io.UnsupportedEncodingException – chetan Nov 15 '10 at 13:26
  • @chetan - try UTF-16, it is equivalent for the first 65535 values. – Andreas Dolk Nov 15 '10 at 13:49
  • This obviously connects to some HTTP service that will handle the encoding and sending to mobile phones. The issue here is that the XML document sent to the service is encoded wrongly, not the sending of the actual message from the service to the mobile phones, which probably will be in UCS-2. – Christoffer Hammarström Nov 15 '10 at 14:36
  • Thank you for guide , i studied the document of API holder and got the answer. – chetan Nov 18 '10 at 07:31
1

You are using your platform's default encoding for the request data, which may very well differ from UTF-8. Try specifying UTF-8 in the OutputStreamWriter:

... new OutputStreamWriter(connection.getOutputStream(), "UTF-8") ...

Another issue is of course that your hand-made XML document will fail as soon as any of your parameters contain characters, which have to be escaped in XML, but that's a different story. Why don't you use an XML library instead?


Just an additional information: The documentation Christoffer points to also explains that the request example you are using is only suitable for text messages with characters in the standard SMS character set. For Unicode character support, you have to use a different request.

jarnbjo
  • 32,366
  • 6
  • 65
  • 87