android 访问web服务端中文编码问题 解决方法

客户端 主要代码

HttpPost httpRequest = new HttpPost(urlAPI);
List <NameValuePair> params = new ArrayList <NameValuePair>();
String x=et1.getText().toString();
String x1=et2.getText().toString();
params.add(new BasicNameValuePair("str",URLEncoder.encode(x)));
params.add(new BasicNameValuePair("str1",URLEncoder.encode(x1)));

 try 
        { 
          /*发叨HTTP request*/
          httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); 
          /*取得HTTP response*/
          HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); 
          /*若状态码为200*/
          if(httpResponse.getStatusLine().getStatusCode() == 200)  
          { 
            /*获取字符串*/
            strResult=EntityUtils.toString(httpResponse.getEntity());   
            if(strResult!=null)
{
System.out.println(strResult);
divideString(strResult);//将字符串拆分为对象
Toast.makeText(MainActivity.this,"登陆成功", Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(MainActivity.this,"用户名和密码不匹配", Toast.LENGTH_SHORT).show();
          } 
          else 
          { 
         Toast.makeText(MainActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
          } 
        } 
        catch (ClientProtocolException e) 
        {  
        Toast.makeText(MainActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
          e.printStackTrace(); 
        } 
        catch (IOException e) 
        {  
        Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show();
          e.printStackTrace(); 
        } 
        catch (Exception e) 
        {  
        Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show();
          e.printStackTrace();  
        }  
         
}



在android通过拆分字符串将其分解出来 。。不知道这样好不好。。

public void divideString(String str)//将字符创拆分为对象
{
String dName="";
String dPwd="";
String dSex="";
String[] str1 = str.split(";");//字符串用分号进行分割 比 111;222;333
int inx = str1[0].indexOf("="); //从=号后面查起返回第一个查到的位置 否则返回-1String name = "tou";
dName = str1[0].substring(inx+1);// 截取从第inx+1(序列)开始的字符串,将其赋值给dName;(从左往右截) //int index = name.indexOf('o');此时 index= 1; //str1[0]=111 str1[1]=222 str1[2]=333
inx= str1[1].indexOf("=");
dPwd=str1[1].substring(inx+1);
inx=str1[2].indexOf("=");
dSex=str1[2].substring(inx+1);
   System.out.println("拆分成功"+dName);
   System.out.println("拆分成功"+dPwd);
   System.out.println("拆分成功"+dSex);
}

web端 加入用这样 。。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
      <%@page import="com.UserMessage.UserMessage" %>
      <%@page import="com.Server.imp.UserServer" %>
      <%@page import="java.net.URLDecoder"%>
<jsp:useBean id="userserver" class="com.Server.imp.UserServer" scope = "session"></jsp:useBean>
<%

String res=URLDecoder.decode(request.getParameter("str"),"utf-8");
String res1=request.getParameter("str1").trim(); 
UserMessage user = new UserMessage();
user.setUsername(res);
user.setUserpwd(res1);
String msg="";
if(userserver.validataUser(res, res1)!=null)
{
user=userserver.validataUser(res, res1);
msg+="name="+res;
msg+=";";
msg+="pwd="+res1;
msg+=";";
msg+="sex="+user.getSex();

}
else
msg="0";
out.println(msg);

%>






我的就好了。。


写的比较简单 熟悉用的人 应该能看懂。。


在写一个项目关于明信片交换的app。   android访问web服务器数据库用的是远程mysql  。

由于android  web 完全自学  遇到好多问题 。。

诚邀爱好者共同学习 。探讨问题。。


QQ7748377335.。


  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值