快递100API接口调用代码示例
package com.util; import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection; public class kuaidi100
{ private static String key = "29833628d495d7a5";//必填项,从官网申请的key public static void main(String[] agrs){ System.out.println(searchkuaiDiInfo("rufengda","516013113118353001"));
try{
URL url= new URL("http://api.kuaidi100.com/api?id=c1441d4e82940df1&com=rufengda&nu=516013113118353001&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(); if (type == null || type.trim().length() == 0 || type.trim().indexOf("text/html") < 0)
return ; if(type.indexOf("charset=") > 0)
charSet = type.substring(type.indexOf("charset=") + 8); byte b[] = new byte[10000];
int numRead = urlStream.read(b);
String content = new String(b, 0, numRead);
while (numRead != -1) {
numRead = urlStream.read(b);
if (numRead != -1) {
//String newContent = new String(b, 0, numRead);
String newContent = new String(b, 0, numRead, charSet);
content += newContent;
}
}
System.out.println("content:" + content);
urlStream.close();
} catch (MalformedURLException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
/**
* 查询快递信息
* @param com 快递公司代码
* @param nu 快递单号
* @return
*/
public static String getExpressInfo(String com ,String nu){
String ret = "";
try{
StringBuilder sb = new StringBuilder();
sb.append("http://api.kuaidi100.com/api?id=");
sb.append(kuaidi100.key);
sb.append("&com=").append(com);
sb.append("&nu=").append(nu);
sb.append("&show=0&muti=1&order=desc");
URL url= new URL(sb.toString());
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(); if (type == null || type.trim().length() == 0 || type.trim().indexOf("text/html") < 0)
return ""; if(type.indexOf("charset=") > 0)
charSet = type.substring(type.indexOf("charset=") + 8); byte b[] = new byte[10000];
int numRead = urlStream.read(b);
String content = new String(b, 0, numRead);
while (numRead != -1) {
numRead = urlStream.read(b);
if (numRead != -1) {
//String newContent = new String(b, 0, numRead);
String newContent = new String(b, 0, numRead, charSet);
content += newContent;
}
}
ret = content;
urlStream.close();
} catch (MalformedURLException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return ret;
}
/**
* 该接口使用与收费的快递公司接口查询
* @param com 快递公司代码
* @param nu 快递单号
* @return
*/
public static String searchkuaiDiInfo(String com, String nu){
String content = "";
try{
StringBuilder sb = new StringBuilder();
sb.append("http://www.kuaidi100.com/applyurl?key=").append(kuaidi100.key);
sb.append("&com=").append(com);
sb.append("&nu=").append(nu);
URL url = new URL(sb.toString());
URLConnection con = url.openConnection();
con.setAllowUserInteraction(false);
InputStream urlStream = url.openStream();
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);
String newContent = new String(b, 0, numRead, "UTF-8");
content += newContent;
}
}
urlStream.close();
}
catch (Exception e){
e.printStackTrace();
System.out.println("快递查询错误");
}
return content;
} }
web项目测试页面的测试代码
package com.kuaidi; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.util.kuaidi100; public class Test extends HttpServlet { private static final long serialVersionUID = 1L; public Test() {
super();
} public void destroy() {
super.destroy();
} public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String expressContent = "";
String expressType = "json";
String exsName = "rufengda";
String exsNum = "516013113118353001"; if ("shunfeng".equals(exsName) || "ems".equals(exsName)) {
//返回HTML页面
expressContent = kuaidi100.searchkuaiDiInfo(exsName, exsNum);
expressType = "html";
}else{
//返回的是json
expressContent = kuaidi100.getExpressInfo(exsName,exsNum);
} request.getSession().setAttribute("expressContent", expressContent);
request.getSession().setAttribute("expressType", expressType); request.getRequestDispatcher("test.jsp").forward(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
} public void init() throws ServletException {
} }
对应的web.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>Test</servlet-name>
<servlet-class>com.kuaidi.Test</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
测试页面test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'test.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> <script src="<%=basePath%>js/jquery-1.11.1.min.js"></script> <script type="text/javascript">
var t = '${expressType}';//返回快递信息的类型
var c = '${expressContent}';//快递内容
$(document).ready(function(){
$('#divEx').show(); if(t=="html"){
if(c!=""){
var content = "<iframe name=\"kuaidi100\" src=\""+c+"\" width=\"520\" height=\"300\"";
content += "marginwidth=\"0\" marginheight=\"0\" hspace=\"0\" vspace=\"0\" frameborder=\"0\" scrolling=\"no\"></iframe>";
$('#divEx').append(content);
}
}
else{//json
if(c!=""){
var content ="<table class=\"tableExpress\" width=\"520px\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" id=\"showtablecontext\">";
content += "<tr><td width=\"27%\" class=\"bluebg\" >时间</td>";
content += "<td width=\"73%\" class=\"bluebg\">地点和跟踪进度</td></tr>"; var info = eval('(' + c + ')');
var tmp = "";
for(var i=0;i<info.data.length;i++){
tmp += "<tr><td class=\"nobg\">";
tmp += info.data[i].time;
tmp += "</td><td class=\"nobg\">";
tmp += info.data[i].context;
tmp += "</td></tr>";
} content += tmp;
content += "</table>";
$('#divEx').append(content);
}
}
});
</script>
</head> <body>
<div id="divEx" style="display:none;height:300px;width:520px;overflow-y: auto;"> </div>
</body>
</html>
快递100API接口调用代码示例的更多相关文章
- 快递鸟API接口调用代码示例(免费不限量)
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...
- EzHttp 流传输调用代码示例
EzHttp框架提供的内置接口,用于文件流等传输 流传输调用代码示例 内置接口: public interface IEzStreamHandler { Task<byte[]> GetD ...
- 快递100API接口开发
api.kuaidi100.com 获得物流单号的跟踪信息(免费) 1.应用场景 2.是否需要授权 3.请求地址 4.输入参数 5.返回结果 6.返回示例 7.API工具 8.FAQ 通过向指定的地址 ...
- 基于php的银行卡实名认证接口调用代码实例
银行卡二元素检测,检测输入的姓名.银行卡号是否一致. 银行卡实名认证接口:https://www.juhe.cn/docs/api/id/188 <?php // +-------------- ...
- 生活常用类API调用的代码示例合集:邮编查询、今日热门新闻查询、区号查询等
以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 邮编查询:通过邮编查询地名:通过地名查询邮编 今日热门新闻查询:提 ...
- 天气类API调用的代码示例合集:全国天气预报、实时空气质量数据查询、PM2.5空气质量指数等
以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 全国天气预报:数据来自国家气象局,可根据地名.经纬度GPS.IP查 ...
- 位置信息类API调用的代码示例合集:中国省市区查询、经纬度地址转换、POI检索等
以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 中国省市区查询:2017最新中国省市区地址 经纬度地址转换:经纬度 ...
- 通讯服务类API调用的代码示例合集:短信服务、手机号归属地查询、电信基站查询等
以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 短信服务:通知类和验证码短信,全国三网合一通道,5秒内到达,费用低 ...
- 开发工具类API调用的代码示例合集:六位图片验证码生成、四位图片验证码生成、简单验证码识别等
以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 六位图片验证码生成:包括纯数字.小写字母.大写字母.大小写混合.数 ...
随机推荐
- go练习2-go的学习资料
好吧 我承认,有自己添加的内容也有从别人的blog 中 ctrl + c 的 官方:http://golang.org ,经常被封 中文手册的翻译:http://code.google.com/p/g ...
- log file sync 事件(转)
log file sync log file sync等待时间发生在redo log从log buffer写入到log file期间. 下面对log file sync做个详细的解释. 何时发 ...
- Android - 采用 SharedPreferences 存储数据
SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data/data/<包名& ...
- postgresql----索引失效
什么是索引失效?如果where过滤条件设置不合理,即使索引存在,且where过滤条件中包含索引列,也会导致全表扫描,索引不起作用.什么条件下会导致索引失效呢? 1.任何计算.函数.类型转换 2.!= ...
- Windows中的DNS服务——正向解析&反向解析配置 分类: AD域 Windows服务 2015-07-16 20:21 19人阅读 评论(0) 收藏
坚信并为之坚持是一切希望的原因. DNS服务是AD域不可或缺的一部分,我们在部署AD域环境时已经搭建了DNS服务(windows server 2008 R2域中的DC部署),但是DNS服务的作用还是 ...
- 为什么 要弄清楚 mysql int(5) int(11) bigint 自建mysql主键id python random 科学计数法
场景: 有1.2亿条问答数据,相同问题的不同答案为不同条的数据,且该表数据逐日递增: 第三方需求(不合理): 将问题.答案数据分别放入问题表.答案表: 问题表的主键为整数,在答案表中,每行数据有相应的 ...
- Windows环境下手动更新boot2docker.iso
GitHub连不上导致自动更新失败. https://github.com/boot2docker/boot2docker/releases 替换了DockerToolbox安装目录和系统盘用户目录\ ...
- 【Python】通过python代码实现demo_test环境的登录,通过csv/txt/excel文件批量添加课程并开启课程操作--(刚开始 项目 页面 模块 元素这种鸟 被称作pageobject 等这些搞完 然后把你的定位器、数据 和脚本在分离 就是传说中那个叫数据驱动 的鸟)
一.1.通过csv文件批量导入数据 1 from selenium import webdriver from time import ctime,sleep import csv #循环读取每一行每 ...
- python修改镜像源
pip升级:python -m pip install --upgrade pip https://www.cnblogs.com/andy9468/p/10319442.html 1.在命令中临时修 ...
- uva 11105 - Semi-prime H-numbers(数论)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/36644069 option=com_onli ...