快递100接口_java

我的案例下载地址:http://download.csdn.net/detail/l294333475/7973501

注意:案例下载完成后只需要替换kuaidi100.java中的key值就可以

servlet层:

package com.liupeng;

import java.io.IOException;
import java.io.PrintWriter;

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 {
	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 = "shunfeng";
		String exsNum = "211001746537";

		//huitongkuaidi 210414794266
		//zhongtong 761284432955
		//shunfeng 211001746537

		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 {
	}

}

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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<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();
  			var nu = '211001746537';
  	  		var name = 'shunfeng';
  	  		var name2 = "";
	  	  	switch(name){
				case "zhongtong":
				name2 = "中通快递";
				break;
			case "shunfeng":
				name2 = "顺丰速递";
				break;
			case "yuantong":
				name2 = "圆通速递";
				break;
			case "ems":
				name2 = "EMS";
				break;
			case "huitongkuaidi":
				name2 = "汇通快递";
				break;
			}
	  	  if(t=="html"){
	  			if(c!=""){
	  				var tmp = "<p class=\"text2\">快递公司:<strong>"+name2+"</strong>;快递单号:<strong>"+nu+"</strong></p>";
		  			var content = "<iframe name=\"kuaidi100\" src=\""+c+"\" width=\"514\" height=\"300\"";
						content += "marginwidth=\"0\" marginheight=\"0\" hspace=\"0\" vspace=\"0\" frameborder=\"0\" scrolling=\"no\"></iframe>";
					$('#divEx').append(tmp);
					$('#divEx').append(content);
				}
	  		}
	  	else{//json
  			if(c!=""){
  				var msg = "<p class=\"text2\">快递公司:<strong>"+name2+"</strong>;快递单号:<strong>"+nu+"</strong></p>";

	  			var content ="<table class=\"tableExpress\" width=\"514px\" 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(msg);
	  			$('#divEx').append(content);
  			}
  		}
  		});
	</script>
  </head>

  <body>
    <div id="divEx" style="display:none;height:300px;width:520px;overflow-y: auto;">

	</div>
  </body>
</html>

kuaidi100.java:

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 = "XXXXXXXX";

	public static void main(String[] agrs)
	{
		//huitongkuaidi 210414794266
		//zhongtong 761284432955
		//shunfeng 211001746537

		System.out.println(searchkuaiDiInfo("shunfeng","211001746537"));
		try
		{
			URL url= new URL("http://api.kuaidi100.com/api?id=c1441d4e82940df1&com=shunfeng&nu=211001746537&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;
    }

}

时间: 2024-10-13 08:03:17

快递100接口_java的相关文章

快递100接口的调用过程

前言 大部分的商城都需要调用快递的接口来记录商城的物流信息,这里就给出一种快递接口(快递100)调用的方法. 正文 一.官方文档 1. 官方文档的地址为: https://www.kuaidi100.com/openapi/api_subscribe.shtml 二.具体实现 1. 商城服务与快递接口服务具体交互示意图: 具体的实现逻辑为: 我们将订单号发给快递100,然后订阅该订单号的消息.当快递100中有订阅信息产生的时候,就会给指定的地址发一个消息去推送类似的消息. 贵公司将需要跟踪的运单

快递100接口开发

1.预约 function kuaidi100Yuyue($company,$number){ $post_data = array(); $post_data["schema"] = 'json' ; //callbackurl请参考callback.php实现,key经常会变,请与快递100联系获取最新key $json_data=array( 'company'=>$company, 'number'=>$number, 'key'=>'key', 'param

物流配送进程快递100查询接口

本插件使得您的网站可以从快递100接口获取相关配送公司的订单的具体进程,并在顾客登录网站时,查看订单详细信息时予以显示. 演示前台: http://coc020.chinaopencart.com 用户名: [email protected] 密码: demo 使用以上信息登录网站前台,查看订单信息,会看到相关快递100接口传送回来的快递进程.

解决快递100普通key不能查询顺丰、申通等快递问题

已在其他板块里和MYCNCART官网插件区提供了下载插件,此贴只是针对快递100的接口进行分析处理--------------------------------------------------------------------------------------------------------------------------------------都知道快递100接口升级后,使用普通的key是无法查询到ems.顺丰.申通等快递公司的物流信息.而需要使用HTMLapi的接口去调用,

免费的快递查询接口

免费的快递查询接口 1.第一个接口只需要快递单号就可以查询.https://sp0.baidu.com/9_Q4sjW91Qh3otqbppnN2DJv/pae/channel/data/asyncqury?cb=jQuery110204759692032715892_1499865778178&appid=4001&com=&nu=运单号.2.第二个接口,需要两个参数,快递公司标识码(参考快递100接口:https://cdn.kuaidi100.com/download/cha

java抓取快递100信息接口

1 package zeze; 2 3 import java.io.IOException; 4 5 import org.json.JSONArray; 6 import org.json.JSONException; 7 import org.json.JSONObject; 8 import org.jsoup.Connection; 9 import org.jsoup.Jsoup; 10 import org.jsoup.nodes.Document; 11 import org.j

快速上手微信小程序-快递100

2007 年 1 月 9 日,乔布斯在旧金山莫斯科尼会展中心发布了首款 iPhone,而在十年后的 1 月 9 日,微信小程序正式上线.张小龙以这样的形式,向乔布斯致敬. 小程序在哪里? 小程序功能模块在"发现"频道最下方的位置.如果没有,请先将微信升级到最新版本,然后在通讯录搜索'小程序示例',点击之后返回"发现频道"即可.Tip:小程序搜索目前不支持模糊查询 小程序会带来什么 无处不在,随时访问 移动互联网的下一站是"唾手可得" --张小龙

快递100物流编码

说明 以下是快递100对外开放的部份公司的列表,同时适用于HtmlAPI和 运单查询API(部份,具体区别请留意快递公司名称后的备注),另外国际类的可以直接点击国际邮政.EMS.快递类列表查看. 由于我们并没有将所有公司完全对外公布,如果在下面没有找到您所要的快递公司,请添加企业QQ 800036857转 小佰或 发邮件至 [email protected] 咨询. 分类 快递公司代码 公司名称 A       auspost 澳大利亚邮政(英文结果)   aae AAE   anxindaku

C#快递跟踪(基于快递100深度定制)

先定义SortList数组用于存放快递公司的简称和全称 public SortedList Ht = new SortedList() { {"zhongyouwuliu", "中邮物流"}, {"zhongtong", "中通速递"}, {"zhongtiewuliu", "中铁快运"}, {"zhaijisong", "宅急送"}, {&quo