0
我有一個web應用程序,它使用apache-tomcat部署的UTF8編碼。在「ąęćńźżół」中使用波蘭語字符發送servlet參數中的數據不是問題。他們解釋得很好。但是,當將字符串發送回CodenameOne時,我得到的只是Ä?一個? ?? ??一個? ??。波蘭語字符的問題CodenameOne和WebService
servlet代碼:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String ID;
String answer = "no answer";
ID = request.getParameter("Id");
answer="ą ę ż ź ć ó";
//this is an answer to the client – Codename one
out.print(answer);
} finally {
out.close();
}
}
CodenameOne代碼:
response = "empty";
try {
//the NetworkManager object
NetworkManager networkManager = NetworkManager.getInstance();
networkManager.start();
networkManager.addErrorListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
NetworkEvent n = (NetworkEvent) evt;
n.getError().printStackTrace();
}});
//ConnectionRequest object
ConnectionRequest request = new ConnectionRequest() {
int chr;
StringBuffer sb = new StringBuffer();
protected void readResponse(InputStream input) throws IOException {
//reading the answer
while ((chr = input.read()) != -1){
sb.append((char) chr);
}
response = sb.toString();
response = response.trim();
}
protected void handleException(Exception err) {
//An error occured - show a message:
Dialog.show("Yikes!!", "Are you connected to the internet? Check your connection", "Ok", null);
}
};
request.setUrl("http://localhost:8080/FirstOneTomcat/Response"); //servlet calling
request.setPost(false);
request.addArgument("ID","ńćżźóąę"); //sending a the parameter Id to the servlet
} catch (Exception e) {
System.out.println(e.getMessage());
}
while(response.equals("empty")) {
//waiting for the answer from the serlvet or jsp server
System.out.println("No response from server");
}
//set the label with the information from the server
findLabelKom().setText(response);
我嵌入波蘭字符只是爲了測試是否回發作品。如果我嘗試在移動設備上運行的CodenameOne應用程序中生成字符串,然後將它們發送到服務器,並嘗試將它們作爲響應發送,結果是可執行的。它們被正確地打印在服務器控制檯中,它們被正確地存儲在數據庫中,但它們被錯誤地返回。 –
這兩種情況都是錯誤的。我建議在雙方都使用\ uation來驗證數據是否中斷。我們經常通過ConnectionRequest將本地化數據傳遞給servlet。 –