顺丰接口路由查询接口
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="在官网"运单资料&签收图",可查看签收人信息" 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>