http post



 

使用HTTP POST 与HTTP GET类似,只是增加了创建包含NameValuePair对象实例的列表,使用UrlEncodedFormEntity包装列表.

 
Java代码  收藏代码

    public String executeHttpPost() throws Exception {  
            BufferedReader in = null;  
            try {  
                // 定义HttpClient  
                HttpClient client = new DefaultHttpClient();  
                  
                   
                // 实例化HTTP方法  
                HttpPost request = new HttpPost("****");  
                  
                // 创建名/值组列表  
                List<NameValuePair> parameters = new ArrayList<NameValuePair>();  
                parameters.add(new BasicNameValuePair("***", "***"));  
                parameters.add(new BasicNameValuePair("***", "***"));  
                  
      
                // 创建UrlEncodedFormEntity对象  
                UrlEncodedFormEntity formEntiry = new UrlEncodedFormEntity(  
                        parameters);  
                request.setEntity(formEntiry);  
                // 执行请求  
                HttpResponse response = client.execute(request);  
      
                in = new BufferedReader(new InputStreamReader(response.getEntity()  
                        .getContent()));  
                StringBuffer sb = new StringBuffer("");  
                String line = "";  
                String NL = System.getProperty("line.separator");  
                while ((line = in.readLine()) != null) {  
                    sb.append(line + NL);  
                }  
                in.close();  
                String result = sb.toString();  
                return result;  
           
            } finally {  
                if (in != null) {  
                    try {  
                        in.close();  
                    } catch (Exception e) {  
                        e.printStackTrace();  
                    }  
                }  
            }  
        }  
    //
    public void register(View v){
        final String name = etname.getText().toString();
        final String pwd = etpwd.getText().toString();
        final String real = etrealname.getText().toString();
        final String email = etmail.getText().toString();
        new Thread(){
            public void run() {
                try{
                    HttpClient client = new DefaultHttpClient();
                    HttpPost post = new HttpPost(HOST+"regist.do");
                    post.setHeader("Content-Type","application/x-www-form-urlencoded");
                    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
                    parameters.add(new BasicNameValuePair("loginname", name));
                    parameters.add(new BasicNameValuePair("password", pwd));
                    parameters.add(new BasicNameValuePair("realname", real));
                    parameters.add(new BasicNameValuePair("email", email));
                    HttpEntity entity = new UrlEncodedFormEntity(parameters );
                    post.setEntity(entity );
                    HttpResponse resp = client.execute(post);
                    String result = EntityUtils.toString(resp.getEntity());
                    Log.d("TAG", "注册结果---->"+result);
                    Message.obtain(handler,100,result).sendToTarget();
                }catch(Exception e){
                    e.printStackTrace();
                }
            };
        }.start();
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值