首页 火车查询 火车查询示例代码 余票查询[Java]

余票查询示例代码[Java]

作者:xiezhongpian 阅读数:1365 上传时间:2017-05-09

余票查询

package api.jisuapi.train;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;

import api.util.HttpUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Ticket {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/train/ticket";
	public static final String start = "杭州";// utf-8
	public static final String end = "北京";// utf-8
	public static final String date = new SimpleDateFormat("yyyy-MM-dd").format("");

	public static void Get() throws Exception {
		String result = null;
		String url = URL + "?appkey=" + APPKEY + "&start=" + URLEncoder.encode(start, "utf-8") + "&end="
				+ URLEncoder.encode(end, "utf-8") + "&date=" + date;

		try {
			result = HttpUtil.sendGet(url, "utf-8");
			JSONObject json = JSONObject.fromObject(result);
			if (json.getInt("status") != 0) {
				System.out.println(json.getString("msg"));
			} else {
				JSONArray resultarr = json.optJSONArray("result");
				for (int i = 0; i < resultarr.size(); i++) {
					JSONObject obj = (JSONObject) resultarr.opt(i);
					String trainno = obj.getString("trainno");
					String type = obj.getString("type");
					String departstation = obj.getString("departstation");
					String terminalstation = obj.getString("terminalstation");
					String station = obj.getString("station");
					String endstation = obj.getString("endstation");
					String day = obj.getString("day");
					String departuretime = obj.getString("departuretime");
					String arrivaltime = obj.getString("arrivaltime");
					String costtime = obj.getString("costtime");
					String numsw = obj.getString("numsw");
					String numtd = obj.getString("numtd");
					String numyd = obj.getString("numyd");
					String numed = obj.getString("numed");
					String numrz = obj.getString("numrz");
					String numyz = obj.getString("numyz");
					String numgr = obj.getString("numgr");
					String numrw = obj.getString("numrw");
					String numyw = obj.getString("numyw");
					String numwz = obj.getString("numwz");
					String numqt = obj.getString("numqt");
					System.out.println(trainno + " " + type + " " + departstation + " " + terminalstation + " "
							+ station + " " + endstation + " " + day + " " + departuretime + " " + arrivaltime + " "
							+ costtime + " " + numsw + " " + numtd + " " + numyd + " " + numed + " " + numrz + " "
							+ numyz + " " + numgr + " " + numrw + " " + numyw + " " + numwz + " " + numqt);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}