前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >通过快递api获取物流信息示例-快递100

通过快递api获取物流信息示例-快递100

作者头像
ZhangXianSheng
发布2019-05-28 12:23:15
9860
发布2019-05-28 12:23:15
举报
文章被收录于专栏:复盘总结文章集合
代码语言:javascript
复制
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;


import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSON;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONArray;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONObject;




public class Kuaidi100 {

private String get(String url) throws ClientProtocolException, IOException{
        DefaultHttpClient httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
        HttpGet httpGet = new HttpGet(url);
        HttpUriRequest request = null;
        request = httpGet;
        HttpContext localContext = new BasicHttpContext();
        HttpResponse response = httpClient.execute(request, localContext);
        return EntityUtils.toString(response.getEntity());
    }

public List<Map<String, String>> queryLogisticsInfoByKuadi100(String trackingNo) {
        try {
            if(StringUtils.isBlank(trackingNo)){
                return null;
            }
            // 1.通过快递单号获取是哪个快递公司。
            String typeResult = this.get("http://www.kuaidi100.com/autonumber/autoComNum?text="+trackingNo);
            JSONObject  typeJsonObject = (JSONObject)JSON.parse(typeResult);
            JSONArray typeDataArray = (JSONArray) typeJsonObject.get("auto");
            if(typeDataArray.isEmpty()){
                return null;
            }
            JSONObject typeObject = (JSONObject)typeDataArray.get(0);
            String type = typeObject.getString("comCode");
            // 2.通过快递公司及快递单号获取物流信息。
            String kuaidiResult = this.get("http://www.kuaidi100.com/query?type="+type+"&postid="+trackingNo);
            JSONObject jsonObject = (JSONObject)JSON.parse(kuaidiResult);
            String status = jsonObject.getString("status");
            if(!"200".equals(status)){
                return null;
            }
            List<Map<String, String>> list = new ArrayList<Map<String, String>>();
            JSONArray dataArray = (JSONArray) jsonObject.get("data");
            for (int i = 0; i < dataArray.size(); i++) {
                Map<String, String> tempMap = new HashMap<String, String>();
                JSONObject temp = (JSONObject) dataArray.get(i);
                tempMap.put("date", (String) temp.get("time"));
                tempMap.put("logisticsInfo", (String) temp.get("context"));
                list.add(tempMap);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档