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

在开始学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

String> 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");

测试代码:

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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值