HttpClient——Post请求

来源:互联网 发布:万思软件开发有限公司 编辑:程序博客网 时间:2024/06/05 02:55
//为以下拼接请求地址public String path="";public String key="";public String pno="";public String ps="";try {        // 创建客户端        HttpClient client = new DefaultHttpClient();        // 创建post对象        HttpPost post = new HttpPost(path);        ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();        list.add(new BasicNameValuePair("key", key));        list.add(new BasicNameValuePair("pno", pno));        list.add(new BasicNameValuePair("ps", ps));        HttpEntity entity = new UrlEncodedFormEntity(list);        // 发送内容        post.setEntity(entity);        // 发送请求        HttpResponse response = client.execute(post);        //得到请求码        int statusCode = response.getStatusLine().getStatusCode();        if (statusCode == 200)         {            // 得到实体内容            InputStream inputStream = response.getEntity()                                .getContent();            int len;            byte[] b = new byte[1024];            ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();            while ((len = inputStream.read(b)) != -1)             {                arrayOutputStream.write(b, 0, len);            }            String strJson = arrayOutputStream.toString();            //handler向主线程发送信息,更新数据                      handler.obtainMessage(SUCCESS, strJson).sendToTarget();            }    } catch (Exception e) {        e.printStackTrace();}
0 0