歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 與 HttpClient 通訊出現亂碼問題的解決

Android 與 HttpClient 通訊出現亂碼問題的解決

日期:2017/3/1 11:08:40   编辑:Linux編程
Android 與 HttpClient 通訊出現亂碼問題的解決

1、Android -> Http Server 組件,比如servlet.


第一種:在HttpEntity時提供編碼,如:
HttpEntity entity = new UrlEncodedFormEntity(params,"GB2312");
第二種:在發送前對數據進行編碼,如:
java.net.URLEncoder.encode(message,"GB2312");
服務器端接收時:
String message = new String (request.getParameter("message").getBytes("iso-8859-1"),"GB2312" )


2、Http Server 比如servlet -> Android

String message = "我的測試消息";
HttpServer 發送時:
message=new String(message.getBytes("GB2312"),"ISO-8859-1");
android 接收時:
message=new String(message.getBytes("ISO-8859-1"),"GB2312");
Copyright © Linux教程網 All Rights Reserved