JAVA 实现阿里云的全球物流快递查询(单号识别)

【单号识别,全球快递查询API接口】全国全球物流信息近500家查询接口,服务器毫秒响应,数据准确。支持中国:顺丰、申通、圆通、韵达、中通、汇通、EMS...等,国外:美,俄,日韩,香港,马来西亚...等快递物流。

一、代码实现

请求参数
在这里插入图片描述
代码示例

public static void main(String[] args) {
    
    
        //请求地址 支持http 和 https 及 WEBSOCKET
        String host = "https://goexpress.market.alicloudapi.com";
        // 后缀
        String path = "/goexpress";
        //拼接请求链接 
        /**
        这里我只跟了订单号没跟公司
        **/
        String urlSend = host + path + "?no=YT6491907189408"+"&type=";
        try {
    
    
            URL url = new URL(urlSend);
            HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection();
             格式Authorization:APPCODE
            httpURLCon.setRequestProperty("Authorization", "APPCODE " + "e8b0bcf5b*********fb2b81****");
            // (中间是英文空格)
            int httpCode = httpURLCon.getResponseCode();
            if (httpCode == 200) {
    
    
                String json = read(httpURLCon.getInputStream());
                System.out.println("正常请求计费(其他均不计费)");
                System.out.println("获取返回的json:");
                System.out.print(json);
                Map<String, Object> map2 = JSON.parseObject(json, Map.class);
                System.out.print(map2);
                Object list = map2.get("list");
                JSONArray objects = JSON.parseArray(list.toString());
                List<Map> maps = objects.toJavaList(Map.class);
                //-----------------------------------这里我打印了具体的信息
                for (Map data : maps) {
    
    
                    System.out.println("time:"+ data.get("time") + "content" + data.get("content"));
                }
            } else {
    
    
                Map<String, List<String>> map = httpURLCon.getHeaderFields();
                String error = map.get("X-Ca-Error-Message").get(0);
                if (httpCode == 400 && "Invalid AppCode `not exists`".equals(error)) {
    
    
                    System.out.println("AppCode错误 ");
                    throw new RRException("AppCode错误 ");
                } else if (httpCode == 400 && "Invalid Url".equals(error)) {
    
    
                    System.out.println("请求的 Method、Path 或者环境错误");
                    throw new RRException("请求的 Method、Path 或者环境错误 ");
                } else if (httpCode == 400 && "Invalid Param Location".equals(error)) {
    
    
                    System.out.println("参数错误");
                    throw new RRException("参数错误");
                } else if (httpCode == 403 && "Unauthorized".equals(error)) {
    
    
                    System.out.println("服务未被授权(或URL和Path不正确)");
                    throw new RRException("服务未被授权(或URL和Path不正确) ");
                } else if (httpCode == 403 && "Quota Exhausted".equals(error)) {
    
    
                    System.out.println("套餐包次数用完 ");
                    throw new RRException("套餐包次数用完 ");
                } else {
    
    
                    System.out.println("参数名错误 或 其他错误");
                    System.out.println(error);
                    throw new RRException(error);
                }
            }

        } catch (MalformedURLException e) {
    
    
            System.out.println("URL格式错误");
            throw new RRException("URL格式错误");
        } catch (UnknownHostException e) {
    
    
            System.out.println("URL地址错误");
            throw new RRException("URL地址错误");
        } catch (Exception e) {
    
    
            // 打开注释查看详细报错异常信息
            e.printStackTrace();
            throw new RRException(e.getMessage());
        }
    }

    /**
     * 读取返回结果
     *
     * @param is
     * @return
     * @throws IOException
     */
    private static String read(InputStream is) throws IOException {
    
    
        StringBuffer sb = new StringBuffer();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        while ((line = br.readLine()) != null) {
    
    
            line = new String(line.getBytes(), "utf-8");
            sb.append(line);
        }
        br.close();
        return sb.toString();
    }

代码结果示例
在这里插入图片描述
官方正常返回示例

{
	"code": "OK",
	"no": "780098068058",
	"type": "ZTO",
	"list": [{
		"content": "【石家庄市】 快件已在 【长安三部】 签收,签收人: 本人, 感谢使用中通快递,期待再次为您服务!",
		"time": "2018-03-09 11:59:26"
	}, {
		"content": "【石家庄市】 快件已到达 【长安三部】(0311-85344265),业务员 容晓光(13081105270) 正在第1次派件, 请保持电话畅通,并耐心等待",
		"time": "2018-03-09 09:03:10"
	}, {
		"content": "【石家庄市】 快件离开 【石家庄】 发往 【长安三部】",
		"time": "2018-03-08 23:43:44"
	}, {
		"content": "【石家庄市】 快件到达 【石家庄】",
		"time": "2018-03-08 21:00:44"
	}, {
		"content": "【广州市】 快件离开 【广州中心】 发往 【石家庄】",
		"time": "2018-03-07 01:38:45"
	}, {
		"content": "【广州市】 快件到达 【广州中心】",
		"time": "2018-03-07 01:36:53"
	}, {
		"content": "【广州市】 快件离开 【广州花都】 发往 【石家庄中转】",
		"time": "2018-03-07 00:40:57"
	}, {
		"content": "【广州市】 【广州花都】(020-37738523) 的 马溪 (18998345739) 已揽收",
		"time": "2018-03-07 00:01:55"
	}],
	"state": "3",  /* -1:单号或代码错误;0:暂无轨迹;1:快递收件;2:在途中;3:签收;4:问题件 5.疑难件 6.退件签收 */
	"msg": "查询成功",
	"name": "中通快递",                 /*  快递公司名称                */  
	"site": "www.zto.com",              /*  快递公司官网                */
	"phone": "95311",                   /*  快递公司电话                */
	"courier": "容晓光",                /*  快递员 或 快递站(没有则为空)*/
        "courierPhone":"13081105270",       /*  快递员电话 (没有则为空)     */
        "updateTime":"2019-08-27 13:56:19", /*  快递轨迹信息最新时间        */
        "takeTime":"2天20小时14分",         /*  发货到收货消耗时长 (截止最新轨迹)  */
	"logo": "https://img3.fegine.com/express/zto.jpg"      /*  快递Logo  */
}

官方失败示例

{"code":"205","no":"7800980680581","type":"ZTO","list":[],"state":"0","msg":"暂无轨迹信息"}

错误代码定义
在这里插入图片描述
更多操作点击进入官方文档查看

猜你喜欢

转载自blog.csdn.net/qq_46100517/article/details/124942787