现在的位置: 首页 > 综合 > 正文

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

2018年04月05日 ⁄ 综合 ⁄ 共 2871字 ⁄ 字号 评论关闭

客户端 主要代码

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.。

  

抱歉!评论已关闭.