3.查看信息
这时候你会发现点击站台信息, 下拉框没有像在手机上面那样展开
你得切换成设备模式***,在下面有切换方法***
4.查找request请求
1.打开“检查”/ 按F12
查看
2.切换设备模式
这时候就能点开了
3.获取request
4.分析返回的信息
[{"@attributes":{"cod":"759\u8def"},"terminal":"\u6caaD-P0705","stopdis":"7","distance":"4302","time":"561"}]
可以确定:
返回的是json数组(有中括号)
distance是到站距离(m)
time是到站时间(s)
4.获得有用信息
URL
Request URL:
https://shanghaicity.openservice.kankanews.com/public/bus/Getstop
必要传递信息
sid:应该指代具体车辆代号
stoptype: 0
stopid: 10.
sid: 7019f275eae92b302744ade1ac88763a
5.使用java代码请求数据
1. 公交信息实体bean
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;
}
2.java代码
注意:
返回的是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"));