4

I am trying to post a message (contains both English and Chinese) to a servlet. if I use Java Application to post the message, it works well.

       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 "æ¸å大学". And I tried to use HttpURLConnection to post a message, the result also contains unrecognized characters. What's the difference between HttpClient in Java Application and HttpClient in Android Application? It's very Weird.

Thanks a lot.

Rob
  • 5,223
  • 5
  • 41
  • 62
zdzapple
  • 59
  • 5
  • once have a look at http://stackoverflow.com/questions/12907683/android-httpclient-defaulthttpclient-httppost/12907782#comment17486368_12907782 it may be useful to you – G_S Oct 17 '12 at 03:24
  • thanks. I've already tried it, but it still post unrecognized characters. I just asked the guy who wrote the Servlet code for help. He said the character encoding is UTF-8, and would send me the servlet code this evenning. Maybe I can find the problem when get the code. I still wondering what's the difference between the HttpPost in 2 application... – zdzapple Oct 17 '12 at 03:59
  • ok if you find the solution be sure to post it so that it will be useful to someone else – G_S Oct 17 '12 at 04:03
  • You're allowed to answer your question. At least that way it will be marked closed. – TJ Thind Oct 19 '12 at 06:55
  • OK :) This is my first time using stackoverflow. Thanks for reminding me. – zdzapple Oct 19 '12 at 14:15

2 Answers2

1

Problem solved. I use Wireshark to capture the packages send to the server. The header of HttpClient in the Java Application is:

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

While the header of the HttpClient in the Android Application is:

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

So I guess the version of HttpClient in android isn't the newest. from Apache HttpClient 4.1 I download a Pre-compiled.jar library and use it in my project. The Problem solved. Thanks all.

Community
  • 1
  • 1
zdzapple
  • 59
  • 5
0

The difference may be in the way Android handles strings.

This may cause a problem if your source code is not in UTF-8: String message = "...";

I would try externalizing the string as

String message = myContext.getString(R.string.message);
full.stack.ex
  • 1,747
  • 2
  • 11
  • 13
  • Thanks. The source code and the project is all in UTF-8. I also tried to use new String(message.getBytes(), "UTF-8)); but it didn't work :( – zdzapple Oct 18 '12 at 01:22
  • Very unlikely that urlencoded (i.e., ASCII) stuff gets distorted in transit. So: 1) try R.string as recommended 2) Really do! 3) Save the output of new UrlEncodedFormEntity(params, "UTF-8") to a variable and compare its value on the two platforms 4) Same for params (the bytes in all the components) 5) Same for message. You need the bytes' numeric values, not anything shown on the screen. Dump them, look at them in a debugger. I would suspect the transport only if all that stuff happened to be equal. – full.stack.ex Oct 18 '12 at 08:08
  • Thanks. Problem solved. I tried a newer version of HttpClient. – zdzapple Oct 19 '12 at 05:58