4

メッセージ (英語と中国語の両方を含む) をサーブレットに投稿しようとしています。Java アプリケーションを使用してメッセージを投稿すると、うまく機能します。

       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;
        }
    } 

Android で HttpPost を使用している間、サーバーは "æ¸å大å¦" のような認識できない文字を取得します。また、HttpURLConnection を使用してメッセージを投稿しようとしましたが、結果にも認識できない文字が含まれています。Java アプリケーションの HttpClient と Android アプリケーションの HttpClient の違いは何ですか? とても奇妙です。

どうもありがとう。

4

2 に答える 2

1

問題が解決しました。Wiresharkを使用して、サーバーに送信されるパッケージをキャプチャします。JavaアプリケーションのHttpClientのヘッダーは次のとおりです。

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

AndroidアプリケーションのHttpClientのヘッダーは次のとおりです。

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

したがって、AndroidのHttpClientのバージョンは最新ではないと思います。ApacheHttpClient4.1から Pre-compiled.jarライブラリをダウンロードしてプロジェクトで使用します。問題は解決しました。皆さんありがとう。

于 2012-10-19T14:17:39.247 に答える
0

違いは、Android が文字列を処理する方法にある可能性があります。

ソース コードが UTF-8 でない場合、これにより問題が発生する可能性があります。

文字列を次のように外部化してみます

String message = myContext.getString(R.string.message);
于 2012-10-17T07:34:28.917 に答える