java访问 某个地级市的天气

1.构造一个天气的模型(类)

  1. public class WeatherInfo {  
  2.     private String date;//时间  
  3.     private String cityname;//城市名  
  4.     private String weather;//天气  
  5.     private String temperature;//气温  
  6.     private String airquality;//pm2.5  
  7.   
  8.     public String getDate() {  
  9.         return date;  
  10.     }  
  11.   
  12.     public void setDate(String date) {  
  13.         this.date = date;  
  14.     }  
  15.   
  16.     public String getCityname() {  
  17.         return cityname;  
  18.     }  
  19.   
  20.     public void setCityname(String cityname) {  
  21.         this.cityname = cityname;  
  22.     }  
  23.   
  24.     public String getWeather() {  
  25.         return weather;  
  26.     }  
  27.   
  28.     public void setWeather(String weather) {  
  29.         this.weather = weather;  
  30.     }  
  31.   
  32.     public String getTemperature() {  
  33.         return temperature;  
  34.     }  
  35.   
  36.     public void setTemperature(String temperature) {  
  37.         this.temperature = temperature;  
  38.     }  
  39.   
  40.     public String getAirquality() {  
  41.         return airquality;  
  42.     }  
  43.   
  44.     public void setAirquality(String airquality) {  
  45.         this.airquality = airquality;  
  46.     }  
  47.   
  48.     @Override  
  49.     public String toString() {  
  50.         return "WeatherInfo [date=" + date + ", cityname=" + cityname  
  51.                 + ", weather=" + weather + ", temperature=" + temperature  
  52.                 + ", airquality=" + airquality + "]";  
  53.     }  
  54.       


2.编写通过天气API获取天气信息和解析数据的的方法类


  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.io.InputStreamReader;  
  5. import java.io.UnsupportedEncodingException;  
  6. import java.net.MalformedURLException;  
  7. import java.net.URL;  
  8. import java.net.URLConnection;  
  9. import java.net.URLEncoder;  
  10. import java.util.zip.GZIPInputStream;  
  11.   
  12. import net.sf.json.JSONArray;  
  13. import net.sf.json.JSONObject;  
  14.   
  15. import org.junit.Test;  
  16.   
  17. /** 
  18.  * 通过get请求向网站http://wthrcdn.etouch.cn/weather_mini获取某个 城市的天气状况数据,数据格式是Json 
  19.  *  
  20.  * @author 22786 
  21.  *  
  22.  */  
  23. public class WeatherUtils {  
  24.     /** 
  25.      * 通过城市名称获取该城市的天气信息 
  26.      *  
  27.      * @param cityName 
  28.      * @return 
  29.      */  
  30.       
  31.     public  static String GetWeatherData(String cityname) {  
  32.         StringBuilder sb=new StringBuilder();;  
  33.         try {  
  34.             //cityname = URLEncoder.encode(cityName, "UTF-8");  
  35.             String weather_url = "http://wthrcdn.etouch.cn/weather_mini?city="+cityname;  
  36.               
  37.   
  38.             URL url = new URL(weather_url);  
  39.             URLConnection conn = url.openConnection();  
  40.             InputStream is = conn.getInputStream();  
  41.             GZIPInputStream gzin = new GZIPInputStream(is);  
  42.             InputStreamReader isr = new InputStreamReader(gzin, "utf-8"); // 设置读取流的编码格式,自定义编码  
  43.             BufferedReader reader = new BufferedReader(isr);  
  44.             String line = null;  
  45.             while((line=reader.readLine())!=null)  
  46.                 sb.append(line+" ");  
  47.             reader.close();  
  48.         } catch (UnsupportedEncodingException e) {  
  49.             // TODO Auto-generated catch block  
  50.             e.printStackTrace();  
  51.         } catch (MalformedURLException e) {  
  52.             // TODO Auto-generated catch block  
  53.             e.printStackTrace();  
  54.         } catch (IOException e) {  
  55.             // TODO Auto-generated catch block  
  56.             e.printStackTrace();  
  57.         }  
  58.         //System.out.println(sb.toString());  
  59.         return sb.toString();  
  60.           
  61.     }  
  62.       
  63.       
  64.     /** 
  65.      * 将JSON格式数据进行解析 ,返回一个weather对象 
  66.      * @param str 
  67.      * @return 
  68.      */  
  69.     public static WeatherInfo GetWeather(String weatherInfobyJson){  
  70.         JSONObject dataOfJson = JSONObject.fromObject(weatherInfobyJson);  
  71.         if(dataOfJson.getInt("status")!=1000)  
  72.             return null;  
  73.           
  74.         //创建WeatherInfo对象,提取所需的天气信息  
  75.         WeatherInfo weatherInfo = new WeatherInfo();  
  76.           
  77.         //从json数据中提取数据  
  78.         String data = dataOfJson.getString("data");  
  79.           
  80.         dataOfJson = JSONObject.fromObject(data);  
  81.         weatherInfo.setCityname(dataOfJson.getString("city"));;  
  82.         weatherInfo.setAirquality(dataOfJson.getString("aqi"));  
  83.           
  84.         //获取预测的天气预报信息  
  85.         JSONArray forecast = dataOfJson.getJSONArray("forecast");  
  86.         //取得当天的  
  87.         JSONObject result=forecast.getJSONObject(0);  
  88.           
  89.         weatherInfo.setDate(result.getString("date"));  
  90.           
  91.         String high = result.getString("high").substring(2);  
  92.         String low  = result.getString("low").substring(2);  
  93.           
  94.         weatherInfo.setTemperature(low+"~"+high);  
  95.           
  96.         weatherInfo.setWeather(result.getString("type"));  
  97.           
  98.           
  99.           
  100.         return weatherInfo;  
  101.     }  
  102. }  

3.测试类

  1. public class Test {  
  2.     public static void main(String[]  args){  
  3.         String info = WeatherUtils.GetWeatherData("武汉");  
  4.         WeatherInfo weatherinfo = WeatherUtils.GetWeather(info);  
  5.         System.out.println(weatherinfo.toString());  
  6.     }  
  7. }  




http://blog.csdn.net/qq_31028891/article/details/78597548

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值