根据API获取天气情况进行解析

package com.nhb;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


/**
 * Get请求天气API 获得json格式数据  将json格式的天气数据解析
 * 使用时请注意:getWeather(int index) 方法的index参数的范围(0-4)
 *             0:代表今天
 *             1:代表明天  
 *              以此类推只能查询五天的天气
 *             返回数据为HashMap对象
 *             取值的时候请注意参数
 *             "city" 代表获取城市名
 *             "date" 代表日期
 *             "quality" 代表天气质量
 *             "pm25"  代表PM2.5
 *             "high"  代表最高温度
 *          "low"   代表最低温度
 *          "weatherType"  代表天气情况
 *          "windScale"   代表风的等级
 *          "windDirection"  代表风的方向
 * @author luck_nhb
 *
 */
public class WeatherQuery {
    private String path = null;
    private String city = null;
    public WeatherQuery(String city) throws IOException {
        //对参数进行编码
        this.city = URLEncoder.encode(city, "UTF-8");
        //拼接链接
        this.path = "http://www.sojson.com/open/api/weather/json.shtml?city="+this.city;
    }
    private String getContent() throws IOException{
        StringBuffer dataBuffer = new StringBuffer();
        URL url = new URL(this.path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        int staus = connection.getResponseCode();
        //判断时候链接成功
        if(staus == 200){
            //获取读取流
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String temp = null;
            while ((temp = bufferedReader.readLine()) != null) {
                dataBuffer.append(temp);
            }
            String content = dataBuffer.toString();
            return content;
        }else{
            System.out.println("获取天气信息失败");
            return null;
        }
    }
    
    /**
     *
     * @param index
     * index 参数从0-4
     * @return
     * 返回HashMap对象
     * @throws IOException
     */
    public HashMap getWeather(int index) throws IOException{
        if (index > 4 ) {
            return null;
        }
        HashMap data = new HashMap();
        String content = getContent();
        //使用FastJson对数据转换和解析
        JSONObject contentJson = JSON.parseObject(content);
        String nowCity = contentJson.getString("city");
        JSONObject partData = contentJson.getJSONObject("data");
        if(index == 0){
            String date = contentJson.getString("date");
            StringBuilder sb = new StringBuilder(date);
            sb.insert(4, "年");
            sb.insert(7, "月");
            date = sb.toString();
            String todayPM = partData.getString("pm25");
            String todayQuality = partData.getString("quality");
            data.put("date", date);
            data.put("quality", todayQuality);
            data.put("pm25",todayPM);
        }
        //获取当前层次下的数组用getJSONArray(key)函数
        JSONArray allData = partData.getJSONArray("forecast");
        JSONObject todayData = allData.getJSONObject(0);
        String weatherType = todayData.getString("type");
        String high = todayData.getString("high");
        String low = todayData.getString("low");
        String windScale = todayData.getString("fl");
        String windDirection = todayData.getString("fx");
        data.put("city", nowCity);
        data.put("high", high);
        data.put("low", low);
        data.put("weatherType", weatherType);
        data.put("windScale", windScale);
        data.put("windDirection", windDirection);
        return data;
    }    
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值