android客户端提交数据到服务器中文乱码三种解决方案.doc_第1页
android客户端提交数据到服务器中文乱码三种解决方案.doc_第2页
android客户端提交数据到服务器中文乱码三种解决方案.doc_第3页
android客户端提交数据到服务器中文乱码三种解决方案.doc_第4页
android客户端提交数据到服务器中文乱码三种解决方案.doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

在开始学android的时候提交数据到服务器出现中文乱码问题,经过多方面查资料终于将漂亮的中文在服务器端获取到了.下面总结出三种方法可已经中文乱码解决.第一种.在客户端适用HttpPost方式提交数据:public static String toCallOnServer(String path,String encode) throws ClientProtocolException, IOExceptionHttpPost 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 =06: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 blocke.printStackTrace(); catch (IOException e) / TODO Auto-generated catch blocke.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 = 06:8080/jiudian/AndroidServlet?action=edit&uname=jack;StringBuffer buffer = new StringBuffer(path);/&question=asdfdsa&answer=asdfdasbuffer.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 blocke.printStackTrace(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); finally try inputStream.close();outputStream.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();return null;服务器代码:String uname = request.getParameter(uname).trim();String question = request.getParameter(question);String answer = request.getParameter(answer);测试代码:String path = http:/19

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论