| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.http.HttpEntity; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.LinkedMultiValueMap; |
| | | import org.springframework.util.MultiValueMap; |
| | | import org.springframework.util.ObjectUtils; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | |
| | | |
| | | List<CityAqi> cityAqiList = new ArrayList<>(); |
| | | |
| | | |
| | | String url = "http://aliv13.data.moji.com/whapi/json/alicityweather/aqi"; |
| | | String appcode = "31b6ea8f804a4472be3b633cfee44849"; |
| | | HttpHeaders httpHeaders = new HttpHeaders(); |
| | | httpHeaders.add("Authorization", "APPCODE " + appcode); |
| | | //请求参数 |
| | | LinkedMultiValueMap<String, Object> requestParams = new LinkedMultiValueMap<>(); |
| | | |
| | | for (CityConfigAqi cityConfigAqi : list) { |
| | | CityAqi cityAqi = new CityAqi(); |
| | | Integer cityCode = cityConfigAqi.getCityCode(); |
| | | Map<String, Object> data; |
| | | try { |
| | | //从第三方接口获取数据 |
| | | data = restTemplate.getForObject("http://sapi.7drlb.com/api/mj?cityID={1}&apiKey=aqi", Map.class, cityConfigAqi.getCityId()); |
| | | requestParams.set("cityId", cityConfigAqi.getCityId()); |
| | | HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(requestParams, httpHeaders); |
| | | ResponseEntity<String> response = restTemplate.postForEntity(url, httpEntity, String.class); |
| | | data = JSONObject.parseObject(response.getBody(), Map.class); |
| | | } catch (Exception e) { |
| | | continue; |
| | | } |