android客户端向服务端传汉字乱码问题

     最近在做android,遇到从android客户端向服务器端发送汉字乱码问题。采用URLConnection的GET方式,在客户端和服

 

务端都需要进行转码,而采用POST方式则不需要转码。具体方法如下:
    
    用URLConnection从android发送数据有两种方式:

     第一种方式:采用get方式传值
        (1)客户端代码:
                  URL url = new URL(mUrl);
                 URLConnection urlConnection = url.openConnection();
                 InputStream is = urlConnection.getInputStream();
                  ByteArrayBuffer baf = new ByteArrayBuffer(50);
                  int current = 0;
                  while ((current = is.read()) != -1) {
                       baf.append((byte) current);
                  }
                  requestInfo = new String(baf.toByteArray(), "UTF-8").trim();
                  is.close();
   
                 对汉字进行处理:
                     URLEncoder.encode(URLEncoder.encode(channelName, "UTF-8"), "UTF-8")

             (2)服务器端接收字段:
 
             URLDecoder.decode(URLDecoder.decode(request.getParameter("nickname"), "UTF-8"), "UTF-8")
   

      第二种方式:采用Post方式:
              客户端代码:
                 public  String sendRemoteRequest(String path,String param){
                             Log.i("lisheng", param.toString());
                             Log.i("lisheng", path);
                             String strRes="";
                              OutputStream os = null;
                             DataOutputStream dos = null;
                            InputStream is = null;
                           BufferedReader br = null;
                           try {
                                    URL url = new URL(path);
                                    URLConnection urlConn = url.openConnection();
                                    urlConn.setDoInput(true);
                                    urlConn.setDoOutput(true);
                                     os = urlConn.getOutputStream();
                                  dos = new DataOutputStream(os);
                                  dos.write(param.getBytes());
                                  dos.flush();
                                  dos.close();
                                  os.close();
                                  is = urlConn.getInputStream();
                                   br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
                                  for (String strLine = ""; (strLine = br.readLine()) != null;)
                                           strRes = (new StringBuilder(String.valueOf(strRes))).append(strLine).toString();
                                 is.close();
                                } catch (MalformedURLException e) {
                                       e.printStackTrace();
                                 } catch (UnsupportedEncodingException e) {
                                      e.printStackTrace();
                                } catch (IOException e) {
                                          e.printStackTrace();
                               }
                                    return strRes;
                              }
                   参数里直接写汉字
      
     服务器端代码:
  
  request.setCharacterEncoding("UTF-8");
  request.getParameter("nickname")
       
  即可得到参数为汉字的值,不需要转码。
          

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值