java调天气接口
可能遇到的問題:
1、導入包錯誤
解決方法:
<dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version><classifier>jdk15</classifier> </dependency>2、
Exception in thread “main” net.sf.json.JSONException: JSONObject[“empty”] not found.
at net.sf.json.JSONObject.getString(JSONObject.java:xxx)
at basicUtils.JSONUtil.main(JSONUtil.java:xx)
解決方法:將JSONObject.getString()換成JSONObject.optString
原因:為什么會報這個錯誤
源碼:
主函數:
方法類:
package cn.bupt.wucw.weather; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.zip.GZIPInputStream;import net.sf.json.JSONArray; import net.sf.json.JSONObject;/*** 通過get請求向網站http://wthrcdn.etouch.cn/weather_mini獲取某個 城市的天氣狀況數據,數據格式是Json* */ public class WeatherUtils {/*** 通過城市名稱獲取該城市的天氣信息* * @param cityName* @return*/public static String GetWeatherData(String cityname) {StringBuilder sb=new StringBuilder();;try {//cityname = URLEncoder.encode(cityName, "UTF-8");String weather_url = "http://wthrcdn.etouch.cn/weather_mini?city="+cityname;URL url = new URL(weather_url);URLConnection conn = url.openConnection();InputStream is = conn.getInputStream();GZIPInputStream gzin = new GZIPInputStream(is);InputStreamReader isr = new InputStreamReader(gzin, "utf-8"); // 設置讀取流的編碼格式,自定義編碼BufferedReader reader = new BufferedReader(isr);String line = null;while((line=reader.readLine())!=null)sb.append(line+" ");reader.close();} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}//System.out.println(sb.toString());return sb.toString();}/*** 將JSON格式數據進行解析 ,返回一個weather對象* @param str* @return*/public static WeatherInfo GetWeather(String weatherInfobyJson){JSONObject dataOfJson = JSONObject.fromObject(weatherInfobyJson);if(dataOfJson.getInt("status")!=1000)return null;//創建WeatherInfo對象,提取所需的天氣信息WeatherInfo weatherInfo = new WeatherInfo();//從json數據中提取數據String data = dataOfJson.getString("data");dataOfJson = JSONObject.fromObject(data);weatherInfo.setCityname(dataOfJson.getString("city"));System.out.println("1111111111111111111111111");weatherInfo.setAirquality(dataOfJson.optString("aqi"));System.out.println("2222222222222222222222222");//獲取預測的天氣預報信息JSONArray forecast = dataOfJson.getJSONArray("forecast");//取得當天的JSONObject result=forecast.getJSONObject(0);weatherInfo.setDate(result.getString("date"));String high = result.getString("high").substring(2);String low = result.getString("low").substring(2);weatherInfo.setTemperature(low+"~"+high);weatherInfo.setWeather(result.getString("type"));return weatherInfo;} }測試類:
package cn.bupt.wucw.weather;public class AppTest {public static void main(String[] args){String info = WeatherUtils.GetWeatherData("海淀");WeatherInfo weatherinfo = WeatherUtils.GetWeather(info);System.out.println(weatherinfo.toString());} }run as:成功
參考文章:
https://www.cnblogs.com/Jackie-zhang/p/6071769.html
https://blog.csdn.net/qq_31028891/article/details/78597548
https://www.cnblogs.com/tc310/p/10718993.html
總結
- 上一篇: 个别手机在微信内无法长按识别二维码
- 下一篇: 修图神器—超简单实现华为HMS ML K