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();
    }

你可能感兴趣的:(http post)