carMonitor.java
4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.bsth.data.zndd;
import org.apache.commons.io.IOUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.*;
@Component
public class carMonitor {
Logger logger = LoggerFactory.getLogger(this.getClass());
static String url;
public List<Map> carMonitor(String lineCode,String directions,String station) {
List<Map> list = new ArrayList<>(); //返回的接口数据
url = "http://58.34.52.130:9777/xxfb/carMonitor?lineid="+lineCode+"&stopid="+station+"&direction="+directions+"&t=";
InputStream in = null;
OutputStream out = null;
try {
HttpURLConnection con = (HttpURLConnection)new URL(url).openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("GET");
con.setConnectTimeout(15000);
con.setReadTimeout(15000);
con.setRequestProperty("keep-alive", "true");
con.setRequestProperty("accept", "application/json");
con.setRequestProperty("content-type", "application/json;charset=UTF-8");
//System.out.println(con.getResponseCode());
//if (con.getResponseCode() == 200) {
in = con.getInputStream();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
IOUtils.copy(in, bout);
//System.out.println(new String(bout.toByteArray()));
if (bout.toByteArray().length == 0){
return list;
}
Document doc = null;
doc = DocumentHelper.parseText(new String(bout.toByteArray(), Charset.forName("UTF-8")));
// 获取根节点
Element rootElt = doc.getRootElement();
//获取到根节点的名称
//System.out.println("根节点:" + rootElt.getName());
Iterator iter = rootElt.elementIterator("cars");
while (iter.hasNext()) {
//获取到第二级节点元所有元素
Element sessionEle = (Element) iter.next();
//System.out.println("-第一级节点名称: " + sessionEle.getName());
//获取到第二级节点指定的子节点
Iterator iterResponse = sessionEle.elementIterator("car");
while (iterResponse.hasNext()){
Map map = new HashMap();
//获取到所有第三级子节点所有元素
Element responseEle = (Element) iterResponse.next();
//System.out.println("--第二级节点名称: "+ responseEle.getName());
//获取到第三级节点指定的子节点
map.put("terminal",responseEle.elementTextTrim("terminal"));
map.put("stopdis",responseEle.elementTextTrim("stopdis"));
map.put("distance",responseEle.elementTextTrim("distance"));
map.put("time",responseEle.elementTextTrim("time"));
map.put("sj",Gpstime(responseEle.elementTextTrim("time")));
map.put("loc",responseEle.elementTextTrim("loc"));
map.put("gpstime",responseEle.elementTextTrim("gpstime"));
map.put("direction",responseEle.elementTextTrim("direction"));
list.add(map);
}
}
//System.out.println(maps.size());
} catch (Exception e) {
logger.error("信息发布接口报错了----", e);
}
return list;
}
public String Gpstime(String duration){
Integer seconds = Integer.parseInt(duration);
Integer hours = seconds / 3600;
Integer minutes = (seconds % 3600) / 60;
Integer remainingSeconds = seconds % 60;
if (hours!= 0){
return hours+"时"+ minutes+"分"+remainingSeconds+"秒";
}else if (minutes != 0){
return minutes+"分"+remainingSeconds+"秒";
}else {
return remainingSeconds+"秒";
}
}
}