您好,欢迎来到爱站旅游。
搜索
您的当前位置:首页android客户端提交数据到服务器中文乱码三种解决方案

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

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

第一种.在客户端适用HttpPost方式提交数据:

public static String toCallOnServer(String path,String encode) throws ClientProtocolException, IOException{

httpPost.addHeader(\"Content-Type\", \"text/html\"); //这行很重要 httpPost.addHeader(\"charset\", HTTP.UTF_8);

//这行很重要

//上述两行制定提交数据的时候通过什么方式来处理URL,第一句,著名这个URL是以

HttpPost httpPost = new HttpPost(pathString +path); DefaultHttpClient client = new DefaultHttpClient(); //httpPost.addHeader(\"charset\ HttpResponse response = client.execute(httpPost);

text/html的格式发送给服务器的(个人理解),第二句,设定url的编码方式.以服务器端一致.在.在这里讲解下URL在传递的过程,首先tomcat在接受URL的时候是以iso-8859-1的编码方式进行编码的,在服务器端接收到数据后要用

new String(name.getBytes(“iso-8859-1),”utf-8”) 这种方式进行转码.

测试代码:

public void testAddQuestion(){

try {

String pathS }

if(response.getStatusLine().getStatusCode()==200){ }

return \"\";

InputStream inputStream = response.getEntity().getContent(); return getContext(inputStream, encode);

=\"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);

// TODO Auto-generated catch block e.printStackTrace();

// TODO Auto-generated catch block

} catch (ClientProtocolException e) {

} catch (IOException e) {

}

}

e.printStackTrace();

服务器端代码:

String msg = \"\";

String uname = request.getParameter(\"uname\").trim(); String question = new

String(request.getParameter(\"question\").getBytes(\"iso-8859-1\"),\"utf-8\"); ;

第二种方式:

适用URL 和HTTPUrlConnetion 连接适用GET方法上传数据

String

answer

=

new

String(request.getParameter(\"answer\").getBytes(\"iso-8859-1\"),\"utf-8\")

URL url = new URL(path);

服务器代码:

String msg = \"\";

String uname = request.getParameter(\"uname\").trim(); String question = new

HttpURLConnection connection = (HttpURLConnection) url

.openConnection();

connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod(\"GET\");

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\")); //

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

第三种方式:

讲数据封装成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(), .append(\"&\");

String result =HttpUtils.loginPostData(path, map); String result = HttpUtils.checkLogin(buffer.toString()); Log.i(TAG, result);

\"utf-8\"))

}

}

}

}

buffer.deleteCharAt(buffer.length() - 1);

byte[] data = buffer.toString().getBytes(); connection.setRequestProperty(\"Content-Type\",

\"application/x-www-form-urlencoded\"); String.valueOf(data.length));

connection.setRequestProperty(\"Content-Length\", outputStream = connection.getOutputStream(); outputStream.write(data);

if (connection.getResponseCode() == 200) { }

// TODO Auto-generated catch block e.printStackTrace();

// TODO Auto-generated catch block e.printStackTrace(); try { }

inputStream.close(); outputStream.close();

// TODO Auto-generated catch block e.printStackTrace();

inputStream = connection.getInputStream(); return getContext(inputStream, \"utf-8\");

} catch (MalformedURLException e) {

} catch (IOException e) {

} finally {

} catch (IOException e) {

return null;

服务器代码:

String uname = request.getParameter(\"uname\").trim();

测试代码:

String question = request.getParameter(\"question\"); String answer = request.getParameter(\"answer\");

String path =

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

Map map = new HashMap(); map.put(\"uname\", \"jack\");

map.put(\"question\", \"你最喜欢的电影\"); map.put(\"answer\", \"哈利波特\");

String result =HttpUtils.loginPostData(path, map);

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- azee.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务