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 them to the clients. My users use "Persian" characters to search their appropriate subjects.
I found that when i send them (persian wordsفارسی
) to the server, it can't recognize them correctly. it works on a stream of " ???? " instead of correct words. It just has this problem with Persian words which are sent from android app not English words. I have never tried other languages but I think it also has this problem with them. I think the mobile device has different code for Persian characters & the C# code which is placed in the server cant translate the Persian words. Let me tell you that I use C# for implementing server side and Java for the android application & the data base is SQL server 2010.
I use query string to catch the parameter. After that I use this parameter to make a query which returns subjects from DB. C#:
string Tag = Request.QueryString["Tag"].ToString();
Java:
this part shows how I send my parameter to the server. I think this is an usual way for sending parameters via HTTP protocol.
HttpURLConnection urlConnection = null;
try{
URL myUrl=new URL("http://10.0.2.2:80/Urgence/SearchResault.aspx?Tag="+Tag);
urlConnection = (HttpURLConnection)myUrl.openConnection();
BufferedReader in = new BufferedReader (new InputStreamReader(urlConnection.getInputStream()));
String temp="";
// Data is used to store Server's Response
while((temp=in.readLine())!=null)
{
Data=Data+temp;
}
}
How should I solve my problem?
short answer
use UTF-8 encoding
If you have a
String message = ...;
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.
Having parameters
set as:
String parameters = "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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments