1. 程式人生 > >快遞100 物流介面對接

快遞100 物流介面對接

  • 到快遞100(https://www.kuaidi100.com/openapi/applyapi.shtml),申請免費版本。
  • 快遞100授權key
  • 快遞查詢API(適用於除EMS、順豐、申通、圓通、中通、韻達之外的公司,可自定義公司與單號,返回xml與json資料,有幾家快遞公司不支援):http://www.kuaidi100.com/openapi/api_post.shtml
  • HtmlAPI(適用於所有包括EMS、順豐、申通、圓通、中通、韻達在內的公司,可自定公司與單號,請求後返回一個url,需要通過iframe來巢狀該url來顯示最終結果,結果下方有快遞100手機端的廣告banner):http://www.kuaidi100.com/download/html_api(20140729).doc
  • JAVA 版本:
  • /**
     * 解析結果封裝成map
     * @param com 快遞公司簡稱
     * @param nu 快遞單號
     */
    public static Map<String,Object> getResultLogistics(String com,String nu){
       Map<String,Object> result = new HashMap<String,Object>();
       String key="2ecf1eadd1cddaa9";//授權KEY 換成自己的即可
       try {
          String str = getLogisticsInformation
    (key,com,nu); if(StringUtil.isEmpty(str)){ result.put("status", "3");//url配置錯誤 result.put("message", "介面異常"); return result; } result.put("kuaidiUrl",str); result.put("status","1"); } catch (Exception e) { log.error(e); e.printStackTrace(); } return
    result; }
    /**
     * 查詢快遞結果
     * @param key 授權密匙(Key)
     * @param com 快遞公司簡稱
     * @param nu 快遞單號
     * @return String
        * @throws Exception
        */
    public static String getLogisticsInformation(String key,String com,String nu) throws Exception{
       String url="http://www.kuaidi100.com/applyurl?key\=%s&com\=%s&nu\=%s";
       url = String.format(url, key,com,nu);
       String result = null;
       HttpClient httpClient = new HttpClient();
       GetMethod getMethod = new GetMethod(url);
       httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
       getMethod.getParams().setParameter("http.method.retry-handler", new DefaultHttpMethodRetryHandler());
       int statusCode = httpClient.executeMethod(getMethod);
       if (statusCode == 200){
          StringBuffer temp = new StringBuffer();
          InputStream in = getMethod.getResponseBodyAsStream();
          BufferedReader buffer = new BufferedReader(
                new InputStreamReader(in, "UTF-8"));
          for (String tempstr = ""; (tempstr = buffer.readLine()) != null;)
             temp = temp.append(tempstr);
          buffer.close();
          in.close();
          result = temp.toString().trim();
       }
       return result;
    }
    JSP頁面展示:
  • <c:if test="${!empty statusMap }">
       <c:forEach items="${statusMap}" var="status">
          <c:if test="${'1'==status.value}">
             <iframe name="kuaidi100" id="kuaidi100" style="margin-left: -100px;" src="${kuaidiUrl}" width="550" height="380" marginwidth="0" marginheight="0"
    hspace="0" vspace="0" frameborder="0" scrolling="no">
    
             </iframe>
          </c:if>
          <c:if test="${'1'!=status.value}">
    
          </c:if>
       </c:forEach>
    </c:if>