1、目的:批量调用顺丰运单路由接口,更新最新物流状态
【1】到顺丰开放平台-服务与支持-开放规范-API-SDK使用-直接下载SDK
【2】开通正式接口必须要完成沙盒的3次成功调用。
2、这里有个坑,沙盒环境也必须替换正式顾客编码和校验码
【1】如果不替换,永远也通不过测试,无法开通正式环境。
3、需要上传电子面单
【1】这里可以在帮助中心提问,跳过审核
4、成功调用后就需要对返回的json数据进行解析
【1】这里又有一个坑,就是json不太规范,需要写两个json解析类,分两次解析
【2】详析代码如下:
class JsonParse{
public String apiResultData;
}
class JsonParse2{
public MsgData msgData;
}
class MsgData{
List<RouteResps> routeResps;
}
class RouteResps{
public String mailNo;
List<Routes> routes;
}
class Routes{
public String acceptTime;
public String remark;
public int opCode;
}
JsonParse jsonParse=gson.fromJson(result,JsonParse.class);
System.out.println("===返回结果:" +jsonParse.apiResultData);
JsonParse2 jsonParse2=gson.fromJson(jsonParse.apiResultData,JsonParse2.class);
5、需要结合官方文档,来判断opCode的值对应的状态
6、接口调用限制每次传10个运单,但是我每次要查2000多个。
【1】所以用循环加逻辑判断来分批调用顺丰的接口
@RequestMapping("updateAhSfTableBySfInterface")
public ModelAndView updateAhSfTableBySfInterface(AhSfTable ahSfTable) throws Exception{
ModelAndView mv=new ModelAndView();
List<AhSfTable> tempAhSfTableList=bigMapper.selectAhSfTableByLastLogisticsStatus();
if(tempAhSfTableList.size()>0){
int needWorkTimes=0;
List<SfInterface> sfInterfaceList=new ArrayList<SfInterface>();
if(tempAhSfTableList.size()%10==0){
//说明刚好可以调整数次接口
needWorkTimes=tempAhSfTableList.size()/10;
//开始分批调用
for(int i=0;i<needWorkTimes;i++){
String tempJsonNeed="";
for(int j=10*i;j<10*(i+1);j++){
tempJsonNeed=tempJsonNeed+"\""+tempAhSfTableList.get(j).getBillCode()+"\",";
}
tempJsonNeed="["+tempJsonNeed.substring(0,tempJsonNeed.length()-1)+"]";
sfInterfaceList=CallExpressAPIUtil.doSfInterfaceQuery(sfInterfaceList,tempJsonNeed);
}
}else{
//说明有剩余,设置为+1次
needWorkTimes=tempAhSfTableList.size()/10+1;
//开始分批调用
for(int i=0;i<needWorkTimes;i++){
String tempJsonNeed="";
for(int j=10*i;j<10*(i+1);j++){
//最后一次会越界,所以加个判断
if(j<tempAhSfTableList.size()){
tempJsonNeed=tempJsonNeed+"\""+tempAhSfTableList.get(j).getBillCode()+"\",";
}
}
tempJsonNeed="["+tempJsonNeed.substring(0,tempJsonNeed.length()-1)+"]";
sfInterfaceList=CallExpressAPIUtil.doSfInterfaceQuery(sfInterfaceList,tempJsonNeed);
}
}
//System.out.println(sfInterfaceList.size());
bigMapper.updateAhSfTableLastLogisticsStatusByBillCode(sfInterfaceList);
/* for(int i=0;i<sfInterfaceList.size();i++){
System.out.println(sfInterfaceList.get(i).getLastLogisticsStatus());
}*/
}
ahSfTable.setOperateResultInfo("更新完成...");
mv.addObject("ahSfTable",ahSfTable);
return mv;
}