1.當服務器給客戶端返回數據是中文時出現亂碼:
(1)服務器時轉換字符串在轉字節的時候能夠指定編碼格式:response.getOutputStream().write("登錄成功".getBytes("UTF-8");
默認碼錶ISO-8859-1裏面沒有包含中文漢字。
(2)客戶端時轉換:String temp = new String("登錄成功","GBK");
2.客戶端app在編輯框輸入中文傳給服務器時接收出現亂碼:
(1)System.out.println("username"+new String(username.getBytes("ISO-8859-1"),"UTF-8"));//把亂的碼轉換成最初的二進制, 再從新組裝這個二進制。(在進行傳送前已經進行了轉換,傳送的是編碼事後的數據)
String path = "http://172.18.22.209:8080/GetTest/servlet/LoginServlet?username"+
urlEncoder.encode(username,"utf-8")+"&password"+urlEncoder.encode(password,"utf-8");
String data = "username="+urlEncoder.encode(username,"utf-8")+"&password="+urlEnoder.encode(password,"utf-8");
(2)httpPost.setEntity(new UrlEncodedFormEntity(pamars, "UTF-8"));node