java 快递100的接口的最简写法~

虽然关于快递100接口 网上有各种各样的写法,我做的时候研究了一段时间,但是都感觉网上的太复杂,而且说的不太清楚,不太好用,最后我采取了这种写法,简洁易懂!大佬们可以忽略~ 

首先你要有个id或者说是key

快递100接口申请

快递100详细参数点击查看

 

上接口~

public interface ExpressService  {
    // com 是快递公司编码
    //num 是快递单号
     String QueryLogistics(String com, String num) throws UnsupportedEncodingException;

}

上实现类~

package com.sixmac.platform.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sixmac.platform.service.ExpressService;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;

@Service
public class ExpressServiceImpl  implements ExpressService {

    public String QueryLogistics(String com, String num) throws UnsupportedEncodingException {
 // com 是快递公司编码
    //num 是快递单号
        String data = "";
        BufferedReader in = null;
        try {
            String postUrl = "http://www.kuaidi100.com/query?id=你自己的id&type=" + com + "&postid=" + num + "&valicode=&temp=0.20180725857720894";
            URL realUrl = new URL(postUrl);
            // 打开和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(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                data += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
            }
        }
        
 
        return data;

    }






}

附上一张快递公司编码图

快递公司编码
快递公司编码

注意: 目前快递100接口不能查询顺丰快递单号

有关顺丰快递查询的接口后面会更新出来~

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值