Android学习(52) -- 使用HttpClient框架做POST提交

发送post请求

    //创建一个客户端对象
    HttpClient client = new DefaultHttpClient();
    //创建一个post请求对象
    HttpPost hp = new HttpPost(path);
  • 往post对象里放入要提交给服务器的数据

    //要提交的数据以键值对的形式存在BasicNameValuePair对象中
    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    BasicNameValuePair bnvp = new BasicNameValuePair("name", name);
    BasicNameValuePair bnvp2 = new BasicNameValuePair("pass", pass);
    parameters.add(bnvp);
    parameters.add(bnvp2);
    //创建实体对象,指定进行URL编码的码表
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "utf-8");
    //为post请求设置实体
    hp.setEntity(entity);
    

核心代码

   public void post(View v){
        EditText et_name = (EditText) findViewById(R.id.et_name);
        EditText et_pass = (EditText) findViewById(R.id.et_pass);

        final String name = et_name.getText().toString();
        final String pass = et_pass.getText().toString();

        Thread t = new Thread(){
            @Override
            public void run() {
                String path = "http://192.168.1.130/Web/servlet/CheckLogin";
                //1.创建客户端对象
                HttpClient hc = new DefaultHttpClient();
                //2.创建post请求对象
                HttpPost hp = new HttpPost(path);

                //封装form表单提交的数据
                BasicNameValuePair bnvp = new BasicNameValuePair("name", name);
                BasicNameValuePair bnvp2 = new BasicNameValuePair("pass", pass);
                List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
                //把BasicNameValuePair放入集合中
                parameters.add(bnvp);
                parameters.add(bnvp2);

                try {
                    //要提交的数据都已经在集合中了,把集合传给实体对象
                    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "utf-8");
                    //设置post请求对象的实体,其实就是把要提交的数据封装至post请求的输出流中
                    hp.setEntity(entity);
                    //3.使用客户端发送post请求
                    HttpResponse hr = hc.execute(hp);
                    if(hr.getStatusLine().getStatusCode() == 200){
                        InputStream is = hr.getEntity().getContent();
                        String text = Utils.getTextFromStream(is);

                        //发送消息,让主线程刷新ui显示text
                        Message msg = handler.obtainMessage();
                        msg.obj = text;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        t.start();

    }

public class Utils {

    public static String getTextFromStream(InputStream is){

        byte[] b = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            while((len = is.read(b)) != -1){
                bos.write(b, 0, len);
            }
            String text = new String(bos.toByteArray());
            bos.close();
            return text;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值