测试代码
返回json格式,xml/html格式自行修改参数
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection; public class kuaidi100 { public static void main(String[] agrs) {
String wuLiu = getWuLiu("huitongkuaidi", "*********");
System.out.println(wuLiu);
}
/**
*
* @author
* @date
* @param com 快递公司代码
* @param nu 物流单号
* @return
*/
public static String getWuLiu(String com,String nu){
String content = null;
try {
/*id:身份授权key,需要申请(此处的key为本人在网上查询)
com:要查询的快递公司代码,不支持中文,自行查看官方文档
nu:要查询的快递单号,请勿带特殊符号,不支持中文(大小写不敏感)
show:返回类型:
0:返回json字符串,
1:返回xml对象,
2:返回html对象,
3:返回text文本。
如果不填,默认返回json字符串。
muti:返回信息数量:
1:返回多行完整的信息,
0:只返回一行信息。
不填默认返回多行。
order:排序:
desc:按时间由新到旧排列,
asc:按时间由旧到新排列。
不填默认返回倒序(大小写不敏感)*/
URL url = new URL(
"http://api.kuaidi100.com/api?id=29833628d495d7a5&com="+com+"&nu="+nu+"&show=0&muti=1&order=desc");
URLConnection con = url.openConnection();
con.setAllowUserInteraction(false);
InputStream urlStream = url.openStream();
String type = con.guessContentTypeFromStream(urlStream);
String charSet = null;
if (type == null)
type = con.getContentType();
//此处的“text/json”与您在show中选择的要一致!!!
if (type == null || type.trim().length() == 0 || type.trim().indexOf("text/json") < 0){
return "";
}
if (type.indexOf("charset=") > 0)
charSet = type.substring(type.indexOf("charset=") + 8); byte b[] = new byte[10000];
int numRead = urlStream.read(b);
content = new String(b, 0, numRead);
while (numRead != -1) {
numRead = urlStream.read(b);
if (numRead != -1) {
String newContent = new String(b, 0, numRead, charSet);
content += newContent;
}
}
urlStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content;
} }
返回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":"已取件,到达 [福建_厦门支公司]"}
]}