在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

批量查询快递单号

2016-7-29 15:47| 发布者: zhangjf| 查看: 642| 评论: 0

摘要: 这是来自快递网一个快递查询的API接口,支持快递单号等的查询。 package com.kuaidi.openapi;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.URL;import java.net ...
这是来自快递网一个快递查询的API接口,支持快递单号等的查询。
                   
            
  1. package com.kuaidi.openapi;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.net.URL;
  5. import java.net.URLConnection;
  6. import java.util.List;
  7. import java.util.Map;
  8. public class Openapi {
  9.         private String app_key = "";
  10.         private String api_url = "http://api.kuaidi.com/openapi.html?";
  11.         private String show = "0";
  12.         private String muti = "0";
  13.         private String order = "desc";
  14.         /**
  15.          * 您获得的快递网接口查询KEY。
  16.          *
  17.          * @param app_key
  18.          *
  19.          */
  20.         public void setApp_key(String app_key) {
  21.                 this.app_key = app_key;
  22.         }
  23.         /**
  24.          * 设置数据返回类型。0: 返回 json 字符串; 1:返回 xml 对象
  25.          *
  26.          * @param show
  27.          *
  28.          */
  29.         public void setShow(String show) {
  30.                 this.show = show;
  31.         }
  32.         /**
  33.          * 设置返回物流信息条目数, 0:返回多行完整的信息; 1:只返回一行信息
  34.          *
  35.          * @param muti
  36.          *
  37.          */
  38.         public void setMuti(String muti) {
  39.                 this.muti = muti;
  40.         }
  41.         /**
  42.          * 设置返回物流信息排序。desc:按时间由新到旧排列; asc:按时间由旧到新排列
  43.          *
  44.          * @param order
  45.          *
  46.          */
  47.         public void setOrder(String order) {
  48.                 this.order = order;
  49.         }
  50.         /**
  51.          *
  52.          * @param nu
  53.          * @param exname
  54.          * @return result
  55.          */
  56.         public String query(String nu, String exname) {
  57.                 String sign = "id=" + app_key + "&com=" + exname + "&nu=" + nu + "&show=" + show + "&muti=" + muti + "&order="
  58.                                 + order;
  59.                 String result = sendGet(api_url + sign);
  60.                 return result;
  61.         }
  62.         /**
  63.          * 向指定URL发送GET方法的请求
  64.          *
  65.          * @param url
  66.          *            发送请求的URL
  67.          * @return URL 所代表远程资源的响应结果
  68.          */
  69.         private static String sendGet(String url) {
  70.                 String result = "";
  71.                 BufferedReader in = null;
  72.                 try {
  73.                         URL realUrl = new URL(url);
  74.                         // 打开和URL之间的连接
  75.                         URLConnection connection = realUrl.openConnection();
  76.                         // 设置通用的请求属性
  77.                         connection.setRequestProperty("accept", "*/*");
  78.                         connection.setRequestProperty("connection", "Keep-Alive");
  79.                         connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  80.                         // 建立实际的连接
  81.                         connection.connect();
  82.                         // 定义 BufferedReader输入流来读取URL的响应
  83.                         in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  84.                         String line;
  85.                         while ((line = in.readLine()) != null) {
  86.                                 result += line;
  87.                         }
  88.                 } catch (Exception e) {
  89.                         System.out.println("发送GET请求出现异常!" + e);
  90.                         e.printStackTrace();
  91.                 }
  92.                 // 使用finally块来关闭输入流
  93.                 finally {
  94.                         try {
  95.                                 if (in != null) {
  96.                                         in.close();
  97.                                 }
  98.                         } catch (Exception e2) {
  99.                                 e2.printStackTrace();
  100.                         }
  101.                 }
  102.                 return result;
  103.         }
  104. }
  105. 1.例子:
  106. package com.kuaidi.openapi;
  107. public class OpenapiExample {
  108.        
  109.         private static String key="************";//官网申请的key
  110.         public static void main(String[] args){
  111.                 Openapi o = new Openapi();
  112.                 o.setApp_key(key);
  113.                 String result =o.query("111111", "quanfengkuaidi");
  114.                 System.out.println(result);
  115.         }
  116. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-4 18:49

Copyright 2015-2025 djqfx

返回顶部