android客户端提交数据到服务器中文乱码三种解决方案

  • 格式:doc
  • 大小:47.50 KB
  • 文档页数:5

在开始学android的时候提交数据到服务器出现中文乱码问题,经过多方面查资料终于将漂亮的中文在服务器端获取到了.下面总结出三种方法可已经中文乱码解决.

第一种.在客户端适用HttpPost方式提交数据: public static String toCallOnServer(String path,String encode) throws ClientProtocolException, IOException{ HttpPost httpPost = new HttpPost(pathString +path); DefaultHttpClient client = new DefaultHttpClient(); //httpPost.addHeader("charset", HTTP.UTF_8); HttpResponse response = client.execute(httpPost);

httpPost.addHeader("Content-Type", "text/html"); //这行很重要 httpPost.addHeader("charset", HTTP.UTF_8); //这行很重要 //上述两行制定提交数据的时候通过什么方式来处理URL,第一句,著名这个URL是以text/html的格式发送给服务器的(个人理解),第二句,设定url的编码方式.以服务器端一致.在.在这里讲解下URL在传递的过程,首先tomcat在接受URL的时候是以iso-8859-1的编码方式进行编码的,在服务器端接收到数据后要用 new String(name.getBytes(“iso-8859-1),”utf-8”) 这种方式进行转码.

if(response.getStatusLine().getStatusCode()==200){ InputStream inputStream = response.getEntity().getContent(); return getContext(inputStream, encode); }

return ""; }

测试代码: public void testAddQuestion(){ try { String pathS ="http://192.168.1.106:8080/jiudian/AndroidServlet?action=edit&uname=jack&question=你最喜欢的电影&answer=哈利波特";

// String newPath = URLEncoder.encode(pathS, "utf-8"); String result = HttpUtils.toCallOnServer(pathS, "utf-8"); Log.i(TAG, result); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

服务器端代码: String msg = ""; String uname = request.getParameter("uname").trim(); String question = new String(request.getParameter("question").getBytes("iso-8859-1"),"utf-8"); String answer = new String(request.getParameter("answer").getBytes("iso-8859-1"),"utf-8");

第二种方式: 适用URL 和HTTPUrlConnetion 连接适用GET方法上传数据

URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("GET");

服务器代码: String msg = ""; String uname = request.getParameter("uname").trim(); String question = new String(request.getParameter("question").getBytes("iso-8859-1"),"utf-8"); String answer = new String(request.getParameter("answer").getBytes("iso-8859-1"),"utf-8"); 测试代码: 测试代码中有需要注意的地方

String path = "http://192.168.1.106:8080/jiudian/AndroidServlet?action=edit&uname=jack";

StringBuffer buffer = new StringBuffer(path); //&question=asdfdsa&answer=asdfdas buffer.append("&question=").append(URLEncoder.encode("你最喜欢的电影", "utf-8")) .append("&answer=").append(URLEncoder.encode("哈利波特", "utf-8")); // String result =HttpUtils.loginPostData(path, map); String result = HttpUtils.checkLogin(buffer.toString()); Log.i(TAG, result);

在测试代码中要把中文乱码进行转换处理

第三种方式: 讲数据封装成map格式,适用URL 和HttpURLConnection连接,用POST方式传递. public static String loginPostData(String path, Map map) {

InputStream inputStream = null; OutputStream outputStream = null; try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); StringBuffer buffer = new StringBuffer(); if (map != null && !map.isEmpty()) { for (Map.Entry entry : map.entrySet()) { buffer.append(entry.getKey()) .append("=") .append(URLEncoder.encode(entry.getValue(), "utf-8")) .append("&"); } buffer.deleteCharAt(buffer.length() - 1); } byte[] data = buffer.toString().getBytes(); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length", String.valueOf(data.length)); outputStream = connection.getOutputStream(); outputStream.write(data); if (connection.getResponseCode() == 200) { inputStream = connection.getInputStream(); return getContext(inputStream, "utf-8"); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { inputStream.close(); outputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }

} return null; }

服务器代码: String uname = request.getParameter("uname").trim(); String question = request.getParameter("question"); String answer = request.getParameter("answer");

测试代码: