JsonService.java
6.61 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.viontech.service.impl;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import com.viontech.model.*;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.viontech.utils.HttpUtils;
import org.springframework.stereotype.Service;
/**
* @author zhaibolin
*/
//@Configuration
@Service
public class JsonService {
@Autowired
private Json jsonclass;
public SimpleWeather getWeatherInfo(int cityId) throws IOException {
String json = getWeatherJson(cityId);
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
WeatherMessage weatherMessage = mapper.readValue(json, WeatherMessage.class);
String jsons = getAqiJson(cityId);
ObjectMapper mappers = new ObjectMapper();
mappers.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mappers.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
WeatherMessage weatherMessage2 = mappers.readValue(jsons, WeatherMessage.class);
SimpleWeather simpleWeather = weathers2SimpleWeather(weatherMessage, weatherMessage2);
return simpleWeather;
} catch (IOException e) {
System.out.println("json解析出错!");
System.out.println(json);
}
return null;
}
private String getWeatherJson(int cityId) throws IOException {
String host = jsonclass.getHost1();
String path = jsonclass.getPath1();
String method = "POST";
String appcode = jsonclass.getAppcode();
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "APPCODE " + appcode);
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
Map<String, String> querys = new HashMap<String, String>();
Map<String, String> bodys = new HashMap<String, String>();
bodys.put("cityId", String.valueOf(cityId));
HttpResponse response = null;
try {
response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
} catch (Exception e) {
e.printStackTrace();
}
return EntityUtils.toString(response.getEntity());
}
private String getAqiJson(int cityId) throws IOException {
String host = jsonclass.getHost2();
String path = jsonclass.getPath2();
String method = "POST";
String appcode = jsonclass.getAppcode();
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "APPCODE " + appcode);
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
Map<String, String> querys = new HashMap<String, String>();
Map<String, String> bodys = new HashMap<String, String>();
bodys.put("cityId", String.valueOf(cityId));
HttpResponse response = null;
try {
response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// return response.toString();
return EntityUtils.toString(response.getEntity());
}
private SimpleWeather weathers2SimpleWeather(WeatherMessage weatherMessage, WeatherMessage weatherMessage2) {
Data data = weatherMessage.getData();
if (data == null) {
return null;
}
data.getCity();
data.getForecast();
City city = data.getCity();
if (city == null) {
return null;
}
Forecast forecast = data.getForecast().get(0);
if (forecast == null) {
return null;
}
SimpleWeather simpleWeather = new SimpleWeather();
simpleWeather.setType(getWeatherCode(forecast.getConditionDay()));
simpleWeather.setData_date(forecast.getPredictDate());
simpleWeather.setHtemp(forecast.getTempDay());
simpleWeather.setLtemp(forecast.getTempNight());
if (weatherMessage2.getData().getAqi() == null) {
simpleWeather.setAqi("0");
} else {
simpleWeather.setAqi(weatherMessage2.getData().getAqi().getValue());
}
simpleWeather.setFengli(forecast.getWindLevelDay());
simpleWeather.setFengxiang(forecast.getWindDirDay());
return simpleWeather;
}
private int getWeatherCode(String weather) {
switch (weather) {
case "晴":
case "大部晴朗":
return 1;
case "多云":
case "少云":
return 2;
case "阴":
return 3;
case "阵雨":
case "局部阵雨":
case "小阵雨":
case "强阵雨":
return 4;
case "雷阵雨":
case "雷电":
case "雷暴":
return 5;
case "雷阵雨伴有冰雹":
case "冰雹":
case "冰粒":
case "冰针":
return 6;
case "雨夹雪":
return 7;
case "小雨":
case "小到中雨":
return 8;
case "中雨":
case "雨":
return 9;
case "大雨":
case "中到大雨":
return 10;
case "暴雨":
case "大暴雨":
case "特大暴雨":
case "大到暴雨":
return 11;
case "阵雪":
case "小阵雪":
return 12;
case "小雪":
return 13;
case "中雪":
case "小到中雪":
case "雪":
return 14;
case "大雪":
return 15;
case "暴雪":
return 16;
case "雾":
case "冻雾":
return 17;
case "冻雨":
return 18;
case "霾":
case "沙尘暴":
case "强沙尘暴":
return 19;
case "浮尘":
case "尘卷风":
case "扬沙":
return 20;
default:
return -1;
}
}
}