HttpClient查找快递

本文介绍了一个使用Java进行HTTP GET和POST请求的例子。演示了如何通过HttpClient发起请求,并处理响应数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//get方法
    public void btGet(View view){
        new Thread(){
            @Override
            public void run() {
                getContent();
            }
        }.start();
    }
 //post方法
    public void btPost(View view){
        new Thread(){
            @Override
            public void run() { postContent("52cb54f14bc3bbcd9a96e1d13ee0cf2b");
            }
        }.start();
    }

   
    public void getContent(){
        try {
            //打开浏览器
            HttpClient httpClient = new DefaultHttpClient();
            //填写一个地址
            HttpGet httpGet = new HttpGet("http://v.juhe.cn/expressonline/getCarriers.php?key=52cb54f14bc3bbcd9a96e1d13ee0cfb&ex_category="+ URLEncoder.encode("圆通","utf-8")+"");
            //开始浏览并等待浏览器响应
            HttpResponse response = httpClient.execute(httpGet);
            //得到状态栏
            int code = response.getStatusLine().getStatusCode();
            if(code == 200){
                InputStream is = response.getEntity().getContent();
                String content = StreamTools.getStream(is);
                System.out.println(content);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   
    public void postContent(String name){
        try {
            //打开浏览器
            HttpClient httpClient = new DefaultHttpClient();
            //给一个网址
            HttpPost httpPost = new HttpPost("http://v.juhe.c/expressonline/getCarriers.php");
            //请求的参数
            List list = new ArrayList();
            list.add(new BasicNameValuePair("key",name));
            list.add(new BasicNameValuePair("ex_category","顺丰"));
            httpPost.setEntity(new UrlEncodedFormEntity(list,"utf-8"));
            //打开网址并等待浏览器响应
            HttpResponse httpResponse = httpClient.execute(httpPost);
            int code = httpResponse.getStatusLine().getStatusCode();
            if(code == 200){
                InputStream is = httpResponse.getEntity().getContent();
                String s = StreamTools.getStream(is);
                //第二种方式
                //String string = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值