赞
踩
冬天到了,楼下759路公交一班20min,实在难受。 微信上的上海发布能看到站时间,感觉还是麻烦,所以用java调用了一下接口,需要看的时候,运行一下代码就行。偷偷懒。
这时候你会发现点击站台信息, 下拉框没有像在手机上面那样展开
你得切换成设备模式,在下面有切换方法
查看
这时候就能点开了
4.分析返回的信息
[{"@attributes":{"cod":"759\u8def"},"terminal":"\u6caaD-P0705","stopdis":"7","distance":"4302","time":"561"}]
可以确定:
返回的是json数组(有中括号)
distance是到站距离(m)
time是到站时间(s)
URL
-
- Request URL:
- https://shanghaicity.openservice.kankanews.com/public/bus/Getstop
必要传递信息
sid:应该指代具体车辆代号
- stoptype: 0
- stopid: 10.
- sid: 7019f275eae92b302744ade1ac88763a
- package com.ybj.cbt.Learn.ThinkingInJava.Ch18.FIleDownload;
-
- import lombok.Getter;
- import lombok.Setter;
-
-
- @Getter
- @Setter
- public class BusInfo {
-
- String attributes;
- String terminal;
- long stopdis;
- String distance;
- String time;
-
-
- }
注意:
返回的是JSONArray ,是一个json对象数组,所以只能用数组解析
- package com.ybj.cbt.Learn.ThinkingInJava.Ch18.FIleDownload;
-
- import org.apache.commons.io.IOUtils;
- import org.springframework.boot.configurationprocessor.json.JSONArray;
- import org.springframework.boot.configurationprocessor.json.JSONObject;
-
-
- import java.net.HttpURLConnection;
- import java.net.URL;
-
- /**
- * @Author getJson
- * @Description //TODO $
- * @Date $ $
- * @Param $
- * @return $
- **/
- public class GetBusInfo {
-
- public static void main(String[] args) throws Exception {
- String path="https://shanghaicity.openservice.kankanews.com/public/bus/Getstop?stoptype=0&stopid=10.&sid=7019f275eae92b302744ade1ac88763a";
- BusInfo busInfo = HttpURLConnection_GET(path);
- System.out.println("亲,759还有"+ Long.valueOf(busInfo.getTime()) /60+"分钟到达顾戴路秀波路 ,距离:"
- +busInfo.getDistance()+"m");
- }
-
-
-
- public static BusInfo HttpURLConnection_GET(String path)throws Exception{
- BusInfo busInfo=new BusInfo();
- URL url=new URL(path);
- HttpURLConnection conn=(HttpURLConnection) url.openConnection();
- conn.setRequestMethod("POST");
- conn.setConnectTimeout(5000);
- if(conn.getResponseCode()==200){
- String jsonString = IOUtils.toString(conn.getInputStream(), "utf-8");
- JSONArray jsonArray=new JSONArray(jsonString);
- for(int i=0;i<jsonArray.length();i++){
- JSONObject jsonObject = jsonArray.getJSONObject(i);
- busInfo.setDistance(jsonObject.getString("distance"));
- busInfo.setTime(jsonObject.getString("time"));
- }
- }
- return busInfo;
- }
-
-
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。