简体   繁体   English

Android中的HttpPost - 无法识别的字符

[英]HttpPost in Android - unrecognized characters

I am trying to post a message (contains both English and Chinese) to a servlet. 我试图将一条消息(包含英文和中文)发布到servlet。 if I use Java Application to post the message, it works well. 如果我使用Java Application发布消息,它运行良好。

       public class PostText
       {
        public final static String HTTP_PORT = "...";

        public static void main(String[] args) throws Exception
        {
            String message = "...";
            System.out.println(post(message));
        }

        public static String post(String message)
        {

            HttpPost httpRequest = new HttpPost(HTTP_PORT);

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("input", message));

            try {
                httpRequest.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

                org.apache.http.HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);


                if (httpResponse.getStatusLine().getStatusCode() == 200) {

                    String strResult = EntityUtils.toString(httpResponse.getEntity());
                    return strResult;
                } else {
                    System.out.println(httpResponse.getStatusLine().getStatusCode());
                }
            } catch (ClientProtocolException e) {
               // ...
            } catch (UnsupportedEncodingException e) {

            } catch (IOException e) {
               // ...
            }
            return null;
        }
    } 

While I use HttpPost in Android, the server will get unrecognized characters, like "æ¸å大å¦". 当我在Android中使用HttpPost时,服务器将获得无法识别的字符,例如“æ¸å大å|”。 And I tried to use HttpURLConnection to post a message, the result also contains unrecognized characters. 我试图使用HttpURLConnection发布消息,结果还包含无法识别的字符。 What's the difference between HttpClient in Java Application and HttpClient in Android Application? Java应用程序中的HttpClient与Android应用程序中的HttpClient有什么区别? It's very Weird. 这很奇怪。

Thanks a lot. 非常感谢。

Problem solved. 问题解决了。 I use Wireshark to capture the packages send to the server. 我使用Wireshark捕获发送到服务器的包。 The header of HttpClient in the Java Application is: Java应用程序中HttpClient的标题是:

User-Agent: Apache-HttpClient/4.2.1(java 1.5)

While the header of the HttpClient in the Android Application is: 虽然Android应用程序中HttpClient的标题是:

User-Agent: Apache-httpclient/unavailable (java 1.4)

So I guess the version of HttpClient in android isn't the newest. 所以我猜Android中的HttpClient版本并不是最新版本。 from Apache HttpClient 4.1 I download a Pre-compiled.jar library and use it in my project. 来自Apache HttpClient 4.1我下载了一个Pre-compiled.jar库并在我的项目中使用它。 The Problem solved. 问题解决了。 Thanks all. 谢谢大家。

The difference may be in the way Android handles strings. 差异可能在于Android处理字符串的方式。

This may cause a problem if your source code is not in UTF-8: String message = "..."; 如果您的源代码不是UTF-8,这可能会导致问题:String message =“...”;

I would try externalizing the string as 我会尝试将字符串外部化为

String message = myContext.getString(R.string.message);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM