メッセージ (英語と中国語の両方を含む) をサーブレットに投稿しようとしています。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 の違いは何ですか? とても奇妙です。
どうもありがとう。