首页 公交地铁查询 公交地铁查询示例代码 换乘查询[Java]

换乘查询示例代码[Java]

作者:xiezhongpian 阅读数:1438 上传时间: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 Station2s {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/transit/station2s";
	public static final String city = "杭州";// utf-8
	public static final String start = "西溪竞舟苑";// utf-8
	public static final String end = "杭州汽车北站";// utf-8
	public static final String type = "";

	public static void Get() throws Exception {
		String result = null;
		String url = URL + "?city=" + URLEncoder.encode(city, "utf-8") + "&start=" + URLEncoder.encode(start, "utf-8")
				+ "&end" + URLEncoder.encode(end, "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 totaldistance = data.getString("totaldistance");
					String totalduration = data.getString("totalduration");
					String totalprice = data.getString("totalprice");
					String arrivetime = data.getString("arrivetime");
					String tiptype = data.getString("tiptype");
					String totalstopnum = data.getString("totalstopnum");
					String totalwalkdistance = data.getString("totalwalkdistance");
					String vehicles = data.getString("vehicles");
					System.out.println(totaldistance + " " + totalduration + " " + totalprice + " " + arrivetime + " "
							+ tiptype + " " + totalstopnum + "" + totalwalkdistance + " " + vehicles);
					if (data.opt("steps") != null) {
						JSONArray step = data.optJSONArray("steps");
						for (int j = 0; j < step.size(); j++) {
							JSONObject steps = (JSONObject) step.opt(j);
							String distance = steps.getString("distance");
							String duration = steps.getString("duration");
							String sname = "";
							if (steps.has("sname")) {
								sname = steps.getString("sname");
							}
							String type = steps.getString("type");
							String vehicle = steps.getString("vehicle");
							String startpoi = steps.getString("startpoi");
							String endpoi = steps.getString("endpoi");
							String steptext = steps.getString("steptext");
							String endname = "";
							if (steps.has("endname")) {
								endname = steps.getString("endname");
							}

							System.out.println(distance + " " + duration + " " + sname + " " + type + " " + vehicle
									+ " " + startpoi + " " + endpoi + " " + steptext + " " + endname);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}