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

Android端:


String name =  “张清山”;

String pwd =     “123456”;


List params = new ArrayList();


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

你可能感兴趣的:(Android,Android,HttpPost)