JAVA — 顺丰路由接口 (Dom4j配合xpath完成xml解析)

顺丰接口路由查询接口

    public static final String SF_CODE = "你的key";
    public static final String SF_CHECKWORD = "你的checkcode";
    public static final String SF_URL = "顺丰提供的url地址";

//运单号,手机号  顺丰查路由需要验证手机号后4位
 public static Map<String,Object> searchSF(String zxOrderSaleGrpno, String phone){
        Map<String,Object> result = new HashMap<>();
        
        StringBuffer sb4 = new StringBuffer();
        sb4.append("<Request service=\'RouteService\' lang=\'zh-CN\'>");
        sb4.append("<Head>"+SF_CODE+"</Head>");
        sb4.append("<Body>");
        sb4.append("<RouteRequest tracking_type=\'1\' check_phoneNo=\'"+phone.substring(7)+"\' tracking_number=\'" + zxOrderSaleGrpno + "\'/>");
        sb4.append("</Body>");
        sb4.append("</Request>");

        HttpClient searchclient = new DefaultHttpClient();
        HttpPost searchclienthttpPost = new HttpPost(SF_URL);
        String verifyCode = Utils.md5EncryptAndBase64(sb4.toString() + SF_CHECKWORD);
        List<NameValuePair> searchparameters = new ArrayList<NameValuePair>();
        searchparameters.add(new BasicNameValuePair("xml", sb4.toString()));
        searchparameters.add(new BasicNameValuePair("verifyCode", verifyCode));
        searchclienthttpPost.setEntity(new UrlEncodedFormEntity(searchparameters, Charset.forName("UTF-8")));
        HttpResponse searchhttpResponse = null;

        try {
            searchhttpResponse = searchclient.execute(searchclienthttpPost);
        } catch (ClientProtocolException e) {
            logger.error(e);
            logger.info("执行接口出现异常!异常信息:" + e.getMessage());
        } catch (IOException e) {
            logger.error(e);
        }

        if (searchhttpResponse.getStatusLine().getStatusCode() == 200) {
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new InputStreamReader(searchhttpResponse.getEntity().getContent(),
                        Charset.forName("UTF-8")));
            } catch (IllegalStateException e) {
                logger.error(e);
            } catch (IOException e) {
                logger.error(e);
            }
            StringBuffer sb5 = new StringBuffer();

            String content = null;
            try {
                while ((content = reader.readLine()) != null) {
                    sb5.append(content);
                }

            } catch (IOException e1) {
                logger.error(e1);
            }

            String searchres = sb5.toString();

            SAXReader saxReader = new SAXReader();
            Document document = null;
            try {
                document = saxReader.read(new ByteArrayInputStream(searchres.getBytes("UTF-8")));
            } catch (DocumentException e) {
                logger.error(e);
            } catch (UnsupportedEncodingException e) {
                logger.error(e);
            }

            Element element=(Element)document.selectSingleNode("//Route[last()]");
            //获取最新状态->已发货->运送中
            Attribute attrOpcode = element.attribute("opcode");// 得到当前节点的属性对象
            String opcode = attrOpcode.getValue();

            Attribute attrAcceptTime = element.attribute("accept_time");// 得到当前节点的属性对象
            String acceptTime = attrAcceptTime.getValue();
            result.put("success","true");
            result.put("opcode",opcode);
            result.put("acceptTime",acceptTime);

        }else{
            result.put("success","false");
        }

        return result;
    }

顺丰返回xml格式

<?xml version='1.0' encoding='UTF-8' ?>
<Response service="RouteService">
	<Head>
		OK
	</Head>
	<Body>
		<RouteResponse mailno="******">
			<Route remark="顺丰速运 已收取快件" accept_time="2019-04-02 21:36:09" accept_address="广州市"
			opcode="54" />
			<Route remark="顺丰速运 已收取快件" accept_time="2019-04-02 21:54:43" accept_address="广州市"
			opcode="50" />
			<Route remark="快件在【广州白云庆丰村营业部】已装车,准备发往 【广州新塘集散中心】" accept_time="2019-04-03 00:33:38"
			accept_address="广州市" opcode="30" />
			<Route remark="快件已发车" accept_time="2019-04-03 00:40:26" accept_address="广州市"
			opcode="36" />
			<Route remark="快件到达 【广州新塘集散中心】" accept_time="2019-04-03 02:48:39" accept_address="广州市"
			opcode="31" />
			<Route remark="快件在【广州新塘集散中心】已装车,准备发往 【深圳总集散中心】" accept_time="2019-04-03 05:07:27"
			accept_address="广州市" opcode="30" />
			<Route remark="快件已发车" accept_time="2019-04-03 08:21:25" accept_address="广州市"
			opcode="36" />
			<Route remark="快件到达 【深圳总集散中心】" accept_time="2019-04-03 09:53:34" accept_address="深圳市"
			opcode="31" />
			<Route remark="快件在【深圳总集散中心】已装车,准备发往 【赣州黄金集散中心】" accept_time="2019-04-03 13:56:28"
			accept_address="深圳市" opcode="30" />
			<Route remark="快件已发车" accept_time="2019-04-03 21:51:08" accept_address="深圳市"
			opcode="36" />
			<Route remark="快件到达 【赣州黄金集散中心】" accept_time="2019-04-04 06:06:20" accept_address="赣州市"
			opcode="31" />
			<Route remark="快件在【赣州黄金集散中心】已装车,准备发往下一站" accept_time="2019-04-04 09:18:24"
			accept_address="赣州市" opcode="30" />
			<Route remark="快件已发车" accept_time="2019-04-04 12:26:35" accept_address="赣州市"
			opcode="36" />
			<Route remark="快件到达 【赣州信丰县橙乡物流园营业点】" accept_time="2019-04-04 13:42:50"
			accept_address="赣州市" opcode="31" />
			<Route remark="正在派送途中,请您准备签收(派件人:邓荣华,电话:*****)" accept_time="2019-04-04 14:30:06"
			accept_address="赣州市" opcode="44" />
			<Route remark="已与收方客户约定新派送时间 201904071627,待派送" accept_time="2019-04-04 16:27:47"
			accept_address="赣州市" opcode="33" />
			<Route remark="正在派送途中,请您准备签收(派件人:***,电话:*****)" accept_time="2019-04-07 07:47:33"
			accept_address="赣州市" opcode="44" />
			<Route remark="快件交给****,正在派送途中(联系电话:*****)" accept_time="2019-04-07 07:50:26"
			accept_address="赣州市" opcode="204" />
			<Route remark="已签收,感谢使用顺丰,期待再次为您服务" accept_time="2019-04-07 11:40:53"
			accept_address="赣州市" opcode="80" />
			<Route remark="在官网&quot;运单资料&amp;签收图&quot;,可查看签收人信息" accept_time="2019-04-07 11:40:54"
			accept_address="赣州市" opcode="8000" />
		</RouteResponse>
	</Body>
</Response>

主要pom依赖


		<dependency>
			<groupId>jaxen</groupId>
			<artifactId>jaxen</artifactId>
			<version>1.1.6</version>
		</dependency>
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

自在如风。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值