Aliyun服务之物流信息查询

其实这个物流信息查询没什么需要写的,因为阿里云官网的demo就可以拿来直接用,我主要是记录一下限制查询次数
阿里云物流查询地址:

官网的demo : 需要填写自己的AppCode和物流单号 就可以直接用了

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;

public class Tools {
    public static void main(String[] args) {
        String host = "https://wuliu.market.alicloudapi.com";// 【1】请求地址 支持http 和 https 及 WEBSOCKET
        String path = "/kdi";  // 【2】后缀
        String appcode = "你自己的AppCode"; // 【3】开通服务后 买家中心-查看AppCode
        String no = "780098068058";// 【4】请求参数,详见文档描述
        String type = "zto"; //  【4】请求参数,不知道可不填 95%能自动识别
        String urlSend = host + path + "?no=" + no +"&type="+type;  // 【5】拼接请求链接
        try {
            URL url = new URL(urlSend);
            HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection();
            httpURLCon .setRequestProperty("Authorization", "APPCODE " + appcode);// 格式Authorization:APPCODE (中间是英文空格)
            int httpCode = httpURLCon.getResponseCode();
            if (httpCode == 200) {
                String json = read(httpURLCon.getInputStream());
                System.out.println("正常请求计费(其他均不计费)");
                System.out.println("获取返回的json:");
                System.out.print(json);
            } else {
                Map<String, List<String>> map = httpURLCon.getHeaderFields();
                String error = map.get("X-Ca-Error-Message").get(0);
                if (httpCode == 400 && error.equals("Invalid AppCode `not exists`")) {
                    System.out.println("AppCode错误 ");
                } else if (httpCode == 400 && error.equals("Invalid Url")) {
                    System.out.println("请求的 Method、Path 或者环境错误");
                } else if (httpCode == 400 && error.equals("Invalid Param Location")) {
                    System.out.println("参数错误");
                } else if (httpCode == 403 && error.equals("Unauthorized")) {
                    System.out.println("服务未被授权(或URL和Path不正确)");
                } else if (httpCode == 403 && error.equals("Quota Exhausted")) {
                    System.out.println("套餐包次数用完 ");
                } else {
                    System.out.println("参数名错误 或 其他错误");
                    System.out.println(error);
                }
            }

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

    }

    /*
     * 读取返回结果
     */
    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();
    }
}

这是官网的返回式例,基本上也没什么毛病.
在这里插入图片描述
这里我对查询做了控制,每次查询之后就回保存物流信息到数据库
(如果有redis等缓存服务器的话,最好是保存在缓存服务器里面),
如果是半小时内重复查询就返回数据库或缓存中的物流信息,因为物流信息没那么快跟新,没必要一直去查,要花钱的,时间间隔可以大一点,2小时,超过两小时再去查询新的物流信息,然后跟新缓存和数据库

@RestController
@Api(tags = {"物流信息查询"})
@CrossOrigin  //跨域支持
@RequestMapping("/app/wuliu")
public class AliyunWuLiuController {

    @Autowired
    AliyunWuLiu aliyunWuLiu;

    @Autowired
    private ISalesOrderHeaderService salesOrderHeaderService;

    @RequestMapping(value = "/findLogistics", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "物流信息查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name="no",value = "物流单号",required=true)
    })
    public Result<String> findLogistics(String no, String type){

        SalesOrderHeader byLogisticsNo = salesOrderHeaderService.findByLogisticsNo(no);
        if (byLogisticsNo ==null || StringUtils.isEmpty(byLogisticsNo.getId())){
            return new ResultUtils<String>().setErrorMsg("查询的物流订单信息不存在!");
        }
        if (StringUtils.isEmpty(byLogisticsNo.getShippingType())){
            String logistics = aliyunWuLiu.findLogistics(no, type);
            byLogisticsNo.setShippingType(logistics);
            byLogisticsNo.setModifiedDate(LocalDateTimeUtil.getLocalDateTime());
            salesOrderHeaderService.update(byLogisticsNo);
            return new ResultUtils<String>().setData(logistics);
        }else {
            LocalDateTime localDateTime = LocalDateTimeUtil.getLocalDateTime();
            LocalDateTime modifiedDate = byLogisticsNo.getModifiedDate();
            Duration duration = Duration.between(modifiedDate,localDateTime);
            long minutes = duration.toMinutes();
            if (minutes <= 30){
                String shippingType = byLogisticsNo.getShippingType();
                return new ResultUtils<String>().setData(shippingType);
            }else {
                String logistics = aliyunWuLiu.findLogistics(no, type);
                byLogisticsNo.setShippingType(logistics);
                byLogisticsNo.setModifiedDate(LocalDateTimeUtil.getLocalDateTime());
                salesOrderHeaderService.update(byLogisticsNo);
                return new ResultUtils<String>().setData(logistics);
            }
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值