Android通过Get方式提交数据以及乱码处理

构建一个发送请求类:

[java]  view plain copy
  1. /** 
  2. *在调用该方法时,也需构建一个Map对象 
  3. * 
  4. */  
  5. public static boolean sendGetRequest(String path, Map<String, String> params, String enc) throws Exception{  
  6.     StringBuilder sb = new StringBuilder(path);  
  7.     sb.append('?');  
  8.     // ?method=save&title=435435435&timelength=89&  
  9.     //把Map中的数据迭代附加到StringBuilder中  
  10.     for(Map.Entry<String, String> entry : params.entrySet()){  
  11.         //URLEncoder.encode对字符串中文进行编码,防止乱码  
  12.         sb.append(entry.getKey()).append('=')  
  13.             .append(URLEncoder.encode(entry.getValue(), enc)).append('&');  
  14.     }  
  15.     //去掉最后一个字符&  
  16.     sb.deleteCharAt(sb.length()-1);  
  17.     //把组拼完的路径传到URL对象  
  18.     URL url = new URL(sb.toString());  
  19.     HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  20.     //设置请求方式,GET要大写  
  21.     conn.setRequestMethod("GET");  
  22.     conn.setConnectTimeout(5 * 1000);  
  23.     //"200"代表请求成功  
  24.     if(conn.getResponseCode()==200){  
  25.         return true;  
  26.     }  
  27.     return false;  
  28. }  

onClick 事件触发:

[java]  view plain copy
  1. public void onClick(View v) {  
  2.     //获取标题  
  3.     String title = titleText.getText().toString();  
  4.     //获取时间长度  
  5.     String timelength = timelengthText.getText().toString();  
  6.     //通过Map构造器传参  
  7.     Map<String, String> params = new HashMap<String, String>();  
  8.     params.put("method""save");  
  9.     //为key赋值  
  10.     params.put("title", title);  
  11.     //为key赋值  
  12.     params.put("timelength", timelength);  
  13.     try {  
  14.         //此处第三个参数,为指定上传数据编码  
  15.         HttpRequest.sendGetRequest("http://192.168.1.100:8080/videoweb/video/manage.do", params, "UTF-8");  
  16.         //提示成功  
  17.         Toast.makeText(MainActivity.this, R.string.success, 1).show();  
  18.     } catch (Exception e) {  
  19.         //提示失败  
  20.         Toast.makeText(MainActivity.this, R.string.error, 1).show();  
  21.         Log.e(TAG, e.toString());  
  22.     }  
  23. }  
 

测试代码:

[java]  view plain copy
  1. VideoForm formbean = (VideoForm)form;  
  2.         if("GET".equals(request.getMethod())){  
  3.             //得到原始数据的二进制数据  
  4.             byte[] data = request.getParameter("title").getBytes("ISO-8859-1");  
  5.             //把二进制数据传给构造器,重新编码为"UTF-8",这样就解决了服务端造成乱码的原因  
  6.             String title = new String(data, "UTF-8");  
  7.             System.out.println("title:"+ title);  
  8.             System.out.println("timelength:"+ formbean.getTimelength());  
  9.         }else{  
  10.             System.out.println("title:"+ formbean.getTitle());  
  11.             System.out.println("timelength:"+ formbean.getTimelength());  
  12.         }  
  

如果上传的数据包含中文时可能会出现乱码,主要原因有两个:

①客户端发送请求时没有对URL进行编码,我们只要使用URLEncoder.encode(keyValue,"UTF-8")处理即可。

②Tomcat服务器在获取参数时,默认编码不是"UTF-8",一般是"ISO-8859-1",此时要如何处理呢?

主要思路是这样的:先把以"ISO-8859-1"(假设是这种编码,如果不是也无所谓)编码获取的数据转为最原始的二进码数据,然后再对该二进制数据以"UTF-8"格式进行重新编码,参考上面代码中的实现应该不难理解。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值