Android Post参数提交 List<NameValuePair>,web端获取数据

来源:互联网 发布:迅雷会员试用一天淘宝 编辑:程序博客网 时间:2024/05/19 00:54

Android端:


String name =  “张清山”;

String pwd =     “123456”;


List<NameValuePair> params = new ArrayList<NameValuePair>();


params.add(new BasicNameValuePair("userName",name)); 

params.add(new BasicNameValuePair("userPwd",pwd)); 


String result;


result = HttpUtils.postRequest(BaseUrl, params);


postRequest是HttpUtils类中的一个静态方法,代码如下:


public static String postRequest(String url,List<NameValuePair> params) throws ClientProtocolException, IOException{  


        //创建HttpPost对象
        HttpPost httpPost = new HttpPost(url);  


//创建请求包体,请求参数放在这里
        //HttpEntity httpEntity = new UrlEncodedFormEntity(params,HTTP.UTF_8);

//httpPost.setEntity(httpEntiry);


        httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));   
        //发送post请求
        HttpResponse httpResponse = httpClient.execute(httpPost);  
          
        //如果服务器成功地返回响应
        if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  
            String result = EntityUtils.toString(httpResponse.getEntity());  
            return result;  
        }  
        return null;  
    }

Web端:


//获取参数:

String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");


Sysout.out.println("用户姓名:" + userName + "用户密码:" + userPwd);


-----------用户姓名:张清山  用户密码:123456

0 0