首页 公交地铁查询 公交地铁查询示例代码 站点查询[Java]

站点查询示例代码[Java]

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

站点查询

package api.jisuapi.transit;

import java.net.URLEncoder;

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

public class Station {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/transit/station";
	public static final int cityid = 382;// 杭州
	public static final String station = "西溪竞舟苑";// utf-8

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

		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 data = (JSONObject) resultarr.opt(i);
					String transitno = data.getString("transitno");
					String startstation = data.getString("startstation");
					String endstation = data.getString("endstation");
					System.out.println(transitno + " " + startstation + " " + endstation);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}