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":"已取件,到达 [福建_厦门支公司]"}
]}

 

posted on 2018-03-07 18:31  你猜怎么着  阅读(4820)  评论(0编辑  收藏  举报

导航