快递100查询物流信息



import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class A {
	
	public static void main(String[] args) {
		List<Map<String, String>> list = queryLogisticsInfoByKuadi100("1125385885101");
		System.out.println(list);
		
	}
	private static 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 static List<Map<String, String>> queryLogisticsInfoByKuadi100(
			String trackingNo) {
		try {
			if (StringUtils.isBlank(trackingNo)) {
				return null;
			}
			// 1.通过快递单号获取是哪个快递公司。
			String typeResult = 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 = 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;
		}
	}
}

结果: 

[
		{
			"date": "2018-11-19 19:46:50",
			"logisticsInfo": "[北京市]投递并签收,签收人:他人收"
		}, {
			"date": "2018-11-18 11:12:23",
			"logisticsInfo": "[北京市]到达 处理中心"
		}, {
			"date": "2018-11-18 01:55:03",
			"logisticsInfo": "离开南通市 发往北京北京市"
		}, {
			"date": "2018-11-13 10:55:25",
			"logisticsInfo": "[南通市]已离开南通,发往南通转运中心"
		}, {
			"date": "2018-11-13 04:31:00",
			"logisticsInfo": "[南通市]已收件"
		}
	]

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值