在Android的解码字符字符、Android

2023-09-07 17:45:32 作者:花落°莫相离

我的工作我的Andr​​oid项目。我要通过我的Andr​​oid应用程序发送一些话给服务器应用程序。之后,我给他们,我将能够搜索到一些调查,并退回给客户。我的用户使用波斯字符,以搜寻其相应的科目。

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.

我发现,当我把他们(波斯语词汇فارسی)的服务器,它不能正确识别它们。它的工作原理,而不是正确的话就流????。它只是有这个问题,有波斯的话这是从Android应用程序不是英语单词发送。我从来没有尝试过其他的语言,但我认为它也有这个问题他们。我认为移动设备具有不同的code波斯文字和放大器;在C#code这是摆在服务器不能转换的波斯语词汇。让我告诉你,我用C#实现服务器端和Java的Andr​​oid应用程序和放大器;该数据库是SQL Server 2010中。

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.

我使用的查询字符串赶上参数。从那以后,我使用这个参数,使返回主题从数据库的查询。 C#:

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的: 这部分显示了我怎么送我的参数给服务器。我认为这是通过HTTP协议发送参数的常用方法。

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?

推荐答案

简短的答案

使用UTF-8 编码

如果你有一个

String message = ...;

这是转换为一个byte []

this is converted to a byte[]

byte[] bytes = message.getBytes();

那么DatagramPacket类必须使用构造

then the DatagramPacket must be constructed using

new DatagramPacket(bytes, bytes.length(), ... );

您调用使用

new DatagramPacket( message.getBytes, message.length(),..,

但使用字符串长度,但波斯需要每个字符多个字节。

but this uses the String length but Farsi requires more than one byte per character.

字符串فارسی有5个字符,但UTF-8编码需要10个字节。您需要发送您的字母表中的10 bytes.One信UTF-8编码需要2个字节,所以这个词有5个字母是连接codeD中的10个字节。数据报数据包的长度必须以字节数给出。

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.

参数设置为:

String parameters = "parameter1=" + URLEncoder.encode("YOUR_STRING_TO_ENCODE","UTF-8");

那么,下AsyncTask的:

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

-movaffagh baash