springboot 整合線上天氣預報
前陣子專案中遇到了需要使用介面查詢天氣介面,之前以為很難,後面發現賊簡單,話不多說直接上程式碼
// 文字轉譯 設定時間超時
public static final String DEF_CHATSET = "UTF-8";
public static final int DEF_CONN_TIMEOUT = 30000;
public static final int DEF_READ_TIMEOUT = 30000;
// 代理商
public static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
Appkey 配置 Appkey 一定是要最真實的
@Value("${forecast.appkey}")
private String APPKEY;
下面是邏輯程式碼
public AjaxJson findForecast(String cityname) {
// 先定義一個結果
String result =null;
String url ="http://apis.juhe.cn/simpleWeather/query";//請求介面地址
Map params = new HashMap();//請求引數
params.put("city",cityname);//要查詢的城市,如:溫州、上海、北京
params.put("key",APPKEY);//應用APPKEY(應用詳細頁查詢)
params.put("dtype","");//返回資料的格式,xml或json,預設json
try {
result =net(url, params, "GET");
} catch (Exception e) {
e.printStackTrace();
}
return AjaxJson.success().put("成功",result);
}
private String net(String strUrl, Map params, String method) throws Exception{
// Object cityname = params.get("city");
// System.out.println(cityname);
HttpURLConnection conn = null;
BufferedReader reader = null;
String rs = null;
try {
StringBuffer sb = new StringBuffer();
if(method==null || method.equals("GET")){
strUrl = strUrl+"?"+urlencode(params);
// System.out.println(strUrl);
}
URL url = new URL(strUrl);
conn = (HttpURLConnection) url.openConnection();
if(method==null || method.equals("GET")){
conn.setRequestMethod("GET");
}else{
conn.setRequestMethod("POST");
conn.setDoOutput(true);
}
conn.setRequestProperty("User-agent", userAgent);
conn.setUseCaches(false);
conn.setConnectTimeout(DEF_CONN_TIMEOUT);
conn.setReadTimeout(DEF_READ_TIMEOUT);
conn.setInstanceFollowRedirects(false);
conn.connect();
if (params!= null && method.equals("POST")) {
try {
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes(urlencode(params));
} catch (Exception e) {
e.printStackTrace();
}
}
InputStream is = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sb.append(strRead);
}
rs = sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
if (conn != null) {
conn.disconnect();
}
}
return rs;
}
public static String urlencode(Map<String,String> data) {
StringBuilder sb = new StringBuilder();
for (Map.Entry i : data.entrySet()) {
try {
sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
以下是執行結果
{
"msg": "操作成功",
"code": 200,
"成功": "{\"reason\":\"查詢成功!\",\"result\":{\"city\":\"成都\",\"realtime\":{\"temperature\":\"6\",\"humidity\":\"93\",\"info\":\"陰\",\"wid\":\"02\",\"direct\":\"北風\",\"power\":\"2級\",\"aqi\":\"45\"},\"future\":[{\"date\":\"2020-12-03\",\"temperature\":\"4\\/6℃\",\"weather\":\"小雨轉陰\",\"wid\":{\"day\":\"07\",\"night\":\"02\"},\"direct\":\"持續無風向\"},{\"date\":\"2020-12-04\",\"temperature\":\"4\\/6℃\",\"weather\":\"陰\",\"wid\":{\"day\":\"02\",\"night\":\"02\"},\"direct\":\"持續無風向\"},{\"date\":\"2020-12-05\",\"temperature\":\"4\\/7℃\",\"weather\":\"陰\",\"wid\":{\"day\":\"02\",\"night\":\"02\"},\"direct\":\"持續無風向\"},{\"date\":\"2020-12-06\",\"temperature\":\"5\\/8℃\",\"weather\":\"小雨\",\"wid\":{\"day\":\"07\",\"night\":\"07\"},\"direct\":\"持續無風向\"},{\"date\":\"2020-12-07\",\"temperature\":\"5\\/9℃\",\"weather\":\"小雨\",\"wid\":{\"day\":\"07\",\"night\":\"07\"},\"direct\":\"持續無風向\"}]},\"error_code\":0}",
"success": true
}
相關文章
- springboot整合mina報錯Spring Boot
- react native天氣預報React Native
- flutter天氣預報APPFlutterAPP
- 天氣預報API介面API
- Flutter實踐:天氣預報Flutter
- [thymeleaf]springboot整合thymeleaf, html使用預置方法Spring BootHTML
- springboot之Druid連線池講解+mybatis整合+PageHelper整合Spring BootUIMyBatis
- 查詢天氣預報網站網站
- 0828-T3 天氣預報
- 5.22 天氣預報系統 小
- SpringBoot專案整合阿里Druid連線池Spring Boot阿里UI
- springboot整合Batis,執行報錯,資料庫連線密碼錯誤Spring BootBAT資料庫密碼
- [TJOI2010] 天氣預報 題解
- Netty(一) SpringBoot 整合長連線心跳機制NettySpring Boot
- Springboot 整合阿里資料庫連線池 druidSpring Boot阿里資料庫UI
- 天氣預報更名“新晴天氣”,品牌升級助力智慧生活
- SpringBoot(19)---SpringBoot整合ApolloSpring Boot
- SpringBoot(17)---SpringBoot整合RocketMQSpring BootMQ
- SpringBoot(十六)_springboot整合JasperReSpring Boot
- SpringBoot整合系列-整合JPASpring Boot
- Mac天氣預報元件:Weather Widget Live for MacMac元件
- 天氣預報:2020年春節出行指南
- 基於Qt的天氣預報專案QT
- 天氣預報API,你想要的它都有API
- 一文帶你看透天氣預報
- 天氣預報到底有什麼作用?
- 天氣預報戰略升級為“新晴天氣”,深耕天氣+出行生活場景
- SpringBoot3整合SpringDoc實現線上介面文件Spring Boot
- springboot專案整合druid資料庫連線池Spring BootUI資料庫
- SpringBoot 整合 rabbitmqSpring BootMQ
- SpringBoot 整合 apolloSpring Boot
- SpringBoot 整合 elkSpring Boot
- SpringBoot 整合 elasticsearchSpring BootElasticsearch
- SpringBoot整合elasticsearchSpring BootElasticsearch
- RocketMQ整合SpringBootMQSpring Boot
- springboot整合redis?Spring BootRedis
- SpringBoot整合MinIOSpring Boot
- Springboot整合pagehelperSpring Boot