Decoding Characters In Android
I am working on my android project. I have to send some words to the server application via my android application. After I send them, I will able to search some surveys and return
Solution 1:
short answer
use UTF-8 encoding
If you have a
Stringmessage= ...;
this is converted to a byte[]
byte[] bytes = message.getBytes();
then the DatagramPacket must be constructed using
new DatagramPacket(bytes, bytes.length(), ... );
Your call uses
new DatagramPacket( message.getBytes, message.length(),..,
but this uses the String length but Farsi requires more than one byte per character.
The string فارسی
has 5 characters, but the UTF-8 encoding requires 10 bytes. You need to send 10 bytes.One letter of your alphabet in UTF-8 encoding requires 2 bytes, so this word with 5 letters is encoded in 10 bytes. Datagram packet lengths must be given in byte counts.
update
Having parameters
set as:
Stringparameters="parameter1=" + URLEncoder.encode("YOUR_STRING_TO_ENCODE","UTF-8");
then, under an AsyncTask:
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setFixedLengthStreamingMode(params.getBytes().length);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(params);
out.close();
String response = "";
Scanner inStream = new Scanner(conn.getInputStream());
while (inStream.hasNextLine()) {
response += (inStream.nextLine());
}
Then, with this, you will got the result from server:
-movaffagh baash
Post a Comment for "Decoding Characters In Android"