Decoding characters in android

curious dog

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ňツ

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Decoding HttpEntity into android string - encoding issue

From Dev

URL decoding in Java for non-ASCII characters

From Dev

Decoding h264 ByteStream on Android

From Dev

Decoding ogg and other compressed audio file formats with Android SDK (OpenSL)

From Dev

Encoding and then decoding json with non-ascii characters in python 2.7

From Dev

Android Code decoding

From Dev

Decoding strings of UTF-16 encoded hex characters

From Dev

Android ZXing waiting time after decoding an image

From Dev

Decoding Urdu Codes in Android

From Dev

Decoding video stream on Android from DJI drone

From Dev

Identify HE-AAC audio on Android for proper decoding and playback

From Dev

JavaScript not correctly encoding and decoding the characters

From Dev

React Javascript displaying/decoding unicode characters

From Dev

Decoding JSON in Java Android

From Dev

Decoding HttpEntity into android string - encoding issue

From Dev

Android Listview and image decoding

From Dev

decoding xml characters in rails view

From Dev

Getting unknown characters while decoding string in c#

From Dev

Decoding strings of UTF-16 encoded hex characters

From Dev

VC++ decoding diacritics characters from stream

From Dev

Android image decoding massive memory consumption

From Dev

Decoding Urdu Codes in Android

From Dev

How to save a web resource, applying decoding to URL Encoded Characters?

From Dev

Decoding url from json on Android

From Dev

Android Wear Message API Encoding and Decoding Byte[]

From Dev

encoding and decoding international characters

From Dev

Reading/Decoding UTF-8 Escape Characters into Native Characters

From Dev

Decoding non standard characters to UTF 8 in Python

From Dev

Decoding Characters as printed in terminal

Related Related

  1. 1

    Decoding HttpEntity into android string - encoding issue

  2. 2

    URL decoding in Java for non-ASCII characters

  3. 3

    Decoding h264 ByteStream on Android

  4. 4

    Decoding ogg and other compressed audio file formats with Android SDK (OpenSL)

  5. 5

    Encoding and then decoding json with non-ascii characters in python 2.7

  6. 6

    Android Code decoding

  7. 7

    Decoding strings of UTF-16 encoded hex characters

  8. 8

    Android ZXing waiting time after decoding an image

  9. 9

    Decoding Urdu Codes in Android

  10. 10

    Decoding video stream on Android from DJI drone

  11. 11

    Identify HE-AAC audio on Android for proper decoding and playback

  12. 12

    JavaScript not correctly encoding and decoding the characters

  13. 13

    React Javascript displaying/decoding unicode characters

  14. 14

    Decoding JSON in Java Android

  15. 15

    Decoding HttpEntity into android string - encoding issue

  16. 16

    Android Listview and image decoding

  17. 17

    decoding xml characters in rails view

  18. 18

    Getting unknown characters while decoding string in c#

  19. 19

    Decoding strings of UTF-16 encoded hex characters

  20. 20

    VC++ decoding diacritics characters from stream

  21. 21

    Android image decoding massive memory consumption

  22. 22

    Decoding Urdu Codes in Android

  23. 23

    How to save a web resource, applying decoding to URL Encoded Characters?

  24. 24

    Decoding url from json on Android

  25. 25

    Android Wear Message API Encoding and Decoding Byte[]

  26. 26

    encoding and decoding international characters

  27. 27

    Reading/Decoding UTF-8 Escape Characters into Native Characters

  28. 28

    Decoding non standard characters to UTF 8 in Python

  29. 29

    Decoding Characters as printed in terminal

HotTag

Archive