登录
原创

快递查询接口DEMO

发布于 2022-07-01 阅读 316
  • Java
原创

pom依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>

代码

package com.example;

import lombok.Data;
import lombok.extern.java.Log;
import net.sf.json.JSONObject;
import org.springframework.web.client.RestTemplate;

/**
* @author micro.cloud.fly
* @date 2022/7/1 10:30 上午
* @desc 快递接口示例
*/
@Log
@Data
public class Demo {
   private final static String URL = "https://v.juhe.cn/exp/index?";
   private final static String query = "com=%s&no=%s&senderPhone=&receiverPhone=&dtype=&key=%s";
   private final static String APP_KEY = "**********需要替换为你的APPKEY**********";
   private String expFirm;
   private String expNum;

   public static void main(String[] args) {
       String expFirm = "**********需要替换为你要查询的的快递公司**********";
       String expNum = "**********需要替换为你要查询的快递单号**********";
       Demo demo = new Demo();
       demo.setExpFirm(expFirm);
       demo.setExpNum(expNum);
       RestTemplate restTemplate = new RestTemplate();
       try {
           JSONObject response = restTemplate.getForObject(URL + String.format(query, demo.expFirm, demo.expNum, APP_KEY), JSONObject.class);
           log.info("接口返回数据:" + response);
           if (response != null && response.containsKey("error_code") && response.getInt("error_code") == 0 && response.containsKey("result")) {

               JSONObject result = response.getJSONObject("result");
               for (Object o : result.keySet()) {
                   log.info(String.format("【%s】==> 【%s】", o.toString(), result.get(o)));
               }
               for (Object o : result.getJSONArray("list")) {
                   log.info(o.toString());
               }
           } else {
               log.warning("请求接口异常!!!");
           }

       } catch (Exception e) {
           log.warning("请求接口错误!!!");
           e.printStackTrace();
       }
   }
}

评论区

眉上的汗水,眉下的泪水,你总要选择一样

0

0

0

举报