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

站站查询示例代码[Java]

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

站站查询

package api.jisuapi.train;

import java.net.URLEncoder;

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

public class Station2s {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/train/station2s";
	public static final String start = "杭州";// utf-8
	public static final String end = "北京";// utf-8
	public static final int ishigh = 0;

	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") + "&ishigh=" + ishigh;

		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 station = obj.getString("station");
					String endstation = obj.getString("endstation");
					String departuretime = obj.getString("departuretime");
					String arrivaltime = obj.getString("arrivaltime");
					String sequenceno = obj.getString("sequenceno");
					String costtime = obj.getString("costtime");
					String distance = obj.getString("distance");
					String isend = obj.getString("isend");
					String pricesw = obj.getString("pricesw");
					String pricetd = obj.getString("pricetd");
					String pricegr1 = obj.getString("pricegr1");
					String pricegr2 = obj.getString("pricegr2");
					String pricerw1 = obj.getString("pricerw1");
					String pricerw2 = obj.getString("pricerw2");
					String priceyw1 = obj.getString("priceyw1");
					String priceyw2 = obj.getString("priceyw2");
					String priceyw3 = obj.getString("priceyw3");
					String priceyd = obj.getString("priceyd");
					String priceed = obj.getString("priceed");
					String pricerz = "";
					if (obj.has("pricerz")) {
						pricerz = obj.getString("pricerz");
					}
					String priceyz = "";
					if (obj.has("priceyz")) {
						priceyz = obj.getString("priceyz");
					}
					System.out.println(trainno + " " + type + " " + station + " " + endstation + " " + departuretime
							+ " " + arrivaltime + " " + sequenceno + " " + costtime + " " + distance + " " + isend + " "
							+ pricesw + " " + pricetd + " " + pricegr1 + " " + pricegr2 + " " + pricerw1 + " "
							+ pricerw2 + " " + priceyw1 + " " + priceyw2 + " " + priceyw3 + " " + priceyd + " "
							+ priceed + " " + pricerz + " " + priceyz);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}