1

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?

Maveňツ
  • 1
  • 12
  • 50
  • 89
curious dog
  • 141
  • 9

1 Answers1

3

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.


update

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

Community
  • 1
  • 1
Maveňツ
  • 1
  • 12
  • 50
  • 89
  • i read your answer 5 times but unfortunately it was not clear enough ! Do me a favor and give me a complete and clear answer . your approximately answer doesn't help me my persian friend ! – curious dog May 07 '15 at 18:54
  • @curiousdog **refigh** check the update :-) `may help you better` !! – Maveňツ May 08 '15 at 05:19
  • 1
    **Refigh** your solution was useful and helped me to solve it . if you are interested in my ultimate result you can visit [link](http://stackoverflow.com/questions/30111820/decoding-utf-issue/30114977#30114977) I appreciate your help **Dadash** – curious dog May 08 '15 at 07:25
  • 1
    *khosh-amadid* @curiousdog I would like to welcome you to [persian chat](http://chat.stackoverflow.com/rooms/54844/persian-chat). – Maveňツ May 08 '15 at 07:31
  • @Obtice me feeling [appy](https://happywishcharity.files.wordpress.com/2014/10/be-happy4.jpg) today – Maveňツ Oct 23 '15 at 11:23