手机查快递,常用快递查询API调用实例
最近小弟在做快递查询类的网站,需要用手机查,于是就从各大快递网站偷来了API接口代码。独乐乐不如众乐乐,在这里分享一下:package com.kuaidi.openapi;
import
import
import
import
import java.util.List;
import java.util.Map;
public class Openapi {
private String app_key = "";
private String api_url = "http://api.
private String show = "0";
private String muti = "0";
private String order = "desc";
/**
* 您获得的快递网接口查询KEY。
*
* @param app_key
*
*/
public void setApp_key(String app_key) {
this.app_key = app_key;
}
/**
* 设置数据返回类型。0: 返回 json 字符串; 1:返回 xml 对象
*
* @param show
*
*/
public void setShow(String show) {
this.show = show;
}
/**
* 设置返回物流信息条目数, 0:返回多行完整的信息; 1:只返回一行信息
*
* @param muti
*
*/
public void setMuti(String muti) {
this.muti = muti;
}
/**
* 设置返回物流信息排序。desc:按时间由新到旧排列; asc:按时间由旧到新排列
*
* @param order
*
*/
public void setOrder(String order) {
this.order = order;
}
/**
*
* @param nu
* @param exname
* @return result
*/
public String query(String nu, String exname) {
String sign = "id=" + app_key + "&com=" + exname + "&nu=" + nu + "&show=" + show + "&muti=" + muti + "&order="
+ order;
String result = sendGet(api_url + sign);
return result;
}
/**
* 向指定URL发送GET方法的请求
*
* @param url
* 发送请求的URL
* @return URL 所代表远程资源的响应结果
*/
private static String sendGet(String url) {
String result = "";
BufferedReader in = null;
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
}
1.例子:
package com.kuaidi.openapi;
public class OpenapiExample {
private static String key="************";//官网申请的key
public static void main(String[] args){
Openapi o = new Openapi();
o.setApp_key(key);
String result =o.query("111111", "quanfengkuaidi");
System.out.println(result);
}
}