Java查询快递物流信息写法【免费】

由于不知道什么时候接口会停止使用,还是推荐大家去购买快递接口。。。

快递110的开放接口 低调使用吧 免费的 亲测可用

代码:

 /**
     * 快递单号查询,使用快递110开放接口完成。
     *
     * @param expressNum 快递单号
     * @return
     * @throws IOException
     */
    public static Map<String, Object> expressCheck(String expressNum) throws IOException {
        // 快递110查询快递公司信息接口
        String comCodeUrl = "http://www.kuaidi100.com/autonumber/autoComNum?resultv2=1&text=" + expressNum;
        JSONObject jsonObject = Demo1.doGet(comCodeUrl);
        if (jsonObject == null) {
            System.out.println("查询失败!");
            return new HashMap<>();
        }
        // 获取快递公司
        String type = jsonObject.getJSONArray("auto").getJSONObject(0).getString("comCode");
        // 快递110查询快递接口
        String expressCheckUrl = "https://www.kuaidi100.com/query?type=" + type + "&postid=" + expressNum;
        return Demo1.doGet(expressCheckUrl).getInnerMap();
    }

主要是上面的两个地址

返回数据。

 

Demo1的doGet方法,就是最简单的一个httpclient get访问

// 方法参考http://hc.apache.org/httpcomponents-client-4.5.x/quickstart.html
public static JSONObject doGet(String uri) throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(uri);
    CloseableHttpResponse response1 = httpclient.execute(httpGet);
    System.out.println(response1.getStatusLine());
    HttpEntity entity1 = response1.getEntity();
    String s = EntityUtils.toString(entity1);
    EntityUtils.consume(entity1);
    response1.close();
    return JSON.parseObject(s);
}

依赖:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.54</version>
</dependency>

还是那句话,自己测试使用吧,如果公司的生产环境不推荐使用这种,不稳定因素太大。。而且一分钱查询一次的接口费用,对公司来说,负担应该不算大。。。
 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值