java物流查詢接口測試代碼-快遞100


測試代碼

返回json格式,xml/html格式自行修改參數

 1 import java.io.IOException;
 2 import java.io.InputStream;
 3 import java.net.MalformedURLException;
 4 import java.net.URL;
 5 import java.net.URLConnection;
 6 
 7 public class kuaidi100 {
 8 
 9     public static void main(String[] agrs) {
10         String wuLiu = getWuLiu("huitongkuaidi", "*********");
11         System.out.println(wuLiu);
12     }
13     /**
14      * 
15      * @author 
16      * @date 
17      * @param com 快遞公司代碼
18      * @param nu 物流單號
19      * @return
20      */
21     public static String getWuLiu(String com,String nu){
22         String content = null;
23         try {
24             /*id:身份授權key,需要申請(此處的key為本人在網上查詢)
25             com:要查詢的快遞公司代碼,不支持中文,自行查看官方文檔
26             nu:要查詢的快遞單號,請勿帶特殊符號,不支持中文(大小寫不敏感)
27             show:返回類型:
28                 0:返回json字符串,
29                 1:返回xml對象,
30                 2:返回html對象,
31                 3:返回text文本。
32                 如果不填,默認返回json字符串。
33             muti:返回信息數量:
34                 1:返回多行完整的信息,
35                 0:只返回一行信息。
36                 不填默認返回多行。
37             order:排序:
38                 desc:按時間由新到舊排列,
39                 asc:按時間由舊到新排列。
40                 不填默認返回倒序(大小寫不敏感)*/ 
41             URL url = new URL(
42                     "http://api.kuaidi100.com/api?id=29833628d495d7a5&com="+com+"&nu="+nu+"&show=0&muti=1&order=desc");
43             URLConnection con = url.openConnection();
44             con.setAllowUserInteraction(false);
45             InputStream urlStream = url.openStream();
46             String type = con.guessContentTypeFromStream(urlStream);
47             String charSet = null;
48             if (type == null)
49                 type = con.getContentType();
50             //此處的“text/json”與您在show中選擇的要一致!!!
51             if (type == null || type.trim().length() == 0 || type.trim().indexOf("text/json") < 0){
52                 return "";
53             }
54             if (type.indexOf("charset=") > 0)
55                 charSet = type.substring(type.indexOf("charset=") + 8);
56 
57             byte b[] = new byte[10000];
58             int numRead = urlStream.read(b);
59             content = new String(b, 0, numRead);
60             while (numRead != -1) {
61                 numRead = urlStream.read(b);
62                 if (numRead != -1) {
63                     String newContent = new String(b, 0, numRead, charSet);
64                     content += newContent;
65                 }
66             }
67             urlStream.close();
68         } catch (MalformedURLException e) {
69             e.printStackTrace();
70         } catch (IOException e) {
71             e.printStackTrace();
72         }
73         return content;
74     }
75 
76 }

返回json格式

{
"message":"ok","status":"1","state":"3","data":[
  {"time":"2012-07-07 13:35:14","context":"客戶已簽收"},
  {"time":"2012-07-07 09:10:10","context":"離開[北京石景山營業廳]派送中,遞送員[溫],電話[]"},
  {"time":"2012-07-06 19:46:38","context":"到達[北京石景山營業廳]"},
  {"time":"2012-07-06 15:22:32","context":"離開[北京石景山營業廳]派送中,遞送員[溫],電話[]"},
  {"time":"2012-07-06 15:05:00","context":"到達[北京石景山營業廳]"},
  {"time":"2012-07-06 13:37:52","context":"離開[北京_同城中轉站]發往[北京石景山營業廳]"},
  {"time":"2012-07-06 12:54:41","context":"到達[北京_同城中轉站]"},
  {"time":"2012-07-06 11:11:03","context":"離開[北京運轉中心駐站班組] 發往[北京_同城中轉站]"},
  {"time":"2012-07-06 10:43:21","context":"到達[北京運轉中心駐站班組]"},
  {"time":"2012-07-05 21:18:53","context":"離開[福建_廈門支公司] 發往 [北京運轉中心_航空]"},
  {"time":"2012-07-05 20:07:27","context":"已取件,到達 [福建_廈門支公司]"}
]}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM