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

来源:互联网 发布:ubuntu grub2引导win7 编辑:程序博客网 时间:2024/06/02 04:31

发送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
原创粉丝点击