Java通过经纬度调用天气接口(YY天气)

调用接口,首先得去注册一个账号,这个账号是我同事注册的,我也只在这里提一下(它只能获取当天的数据,没有百度API的功能强大),但是JAVA实现的方法和思路都是一样的

package com.common.filo;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class weatherApi {

    public static void main(String[] args) {
        //31.874000,101.110880,3362.300
        String weidu = "101.110880";
        String jindu = "31.874000";

        String weatherData = getWeatherData(weidu,jindu);
        List getWeather = GetWeather(weatherData);
        for (Object object : getWeather) {
            System.out.println(object.toString());
        }


    }
    /**
     * 根据经纬度获取城市的气温信息
     * @param weidu 纬度
     * @param jindu 经度
     * @return
     */
    public static String getWeatherData(String weidu,String jindu) {
        StringBuilder sb = new StringBuilder();
        //http://api.yytianqi.com/weatherhours?key=bopi3li1ip93ae0n&city=31.874000,101.110880"
        try {
            String weather_url = "http://api.yytianqi.com/weatherhours?key=bopi3li1ip93ae0n&city="+weidu+","+jindu+"";
            URL url = new URL(weather_url);
            URLConnection conn = url.openConnection();
            InputStream inputStream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"utf-8");
            BufferedReader reader = new BufferedReader(inputStreamReader);
            String line = null;
            while((line=reader.readLine())!=null) {
                sb.append(line+" ");
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    /**
     * 对调用天气API的数据进行json序列化
     * @param weatherJson
     * @return
     */
    public static List GetWeather(String weatherJson) {
        //城市名
        String cityname= "";
        //获取当时的气温
        String qw = "";
        List list = new ArrayList();
        JSONObject dataJson = JSONObject.fromObject(weatherJson.trim());
        String getCode = dataJson.getString("msg");
        //如果没有获取到信息,则返回未知
        if(getCode.equals("Error")) {
            cityname = "未知";
            qw = "未知";
            list.add(cityname);
            list.add(qw);
            return list;
        }else {
            String data = dataJson.getString("data");
            dataJson = JSONObject.fromObject(data);
            cityname = dataJson.getString("cityName");
            JSONArray qwlist = dataJson.getJSONArray("list");
            JSONObject result = qwlist.getJSONObject(0);
            qw = result.getString("qw");
            qw = qw+"℃";
            list.add(cityname);
            list.add(qw);
        }
            return list;
    } 
}

调用API获取到的数据:

{"code":1,"msg":"Sucess","counts":19921,"data":{"cityId":"CH271911","cityName":"壤塘","sj":"2018-05-17 12:00:00","list":[{"tq":"阴","numtq":"02","qw":"11","sd":"75","sj":"2018-05-17 12:00:00"},{"tq":"阵雨","numtq":"03","qw":"12","sd":"72","sj":"2018-05-17 13:00:00"},{"tq":"阵雨","numtq":"03","qw":"13","sd":"70","sj":"2018-05-17 14:00:00"},{"tq":"阴","numtq":"02","qw":"13","sd":"70","sj":"2018-05-17 15:00:00"},{"tq":"多云","numtq":"01","qw":"13","sd":"70","sj":"2018-05-17 16:00:00"},{"tq":"多云","numtq":"01","qw":"13","sd":"70","sj":"2018-05-17 17:00:00"},{"tq":"多云","numtq":"01","qw":"13","sd":"72","sj":"2018-05-17 18:00:00"},{"tq":"阵雨","numtq":"03","qw":"12","sd":"74","sj":"2018-05-17 19:00:00"},{"tq":"阵雨","numtq":"03","qw":"10","sd":"77","sj":"2018-05-17 20:00:00"},{"tq":"小雨","numtq":"07","qw":"9","sd":"80","sj":"2018-05-17 21:00:00"},{"tq":"小雨","numtq":"07","qw":"9","sd":"84","sj":"2018-05-17 22:00:00"},{"tq":"小雨","numtq":"07","qw":"8","sd":"88","sj":"2018-05-17 23:00:00"},{"tq":"多云","numtq":"01","qw":"7","sd":"88","sj":"2018-05-18 00:00:00"},{"tq":"多云","numtq":"01","qw":"7","sd":"89","sj":"2018-05-18 01:00:00"},{"tq":"多云","numtq":"01","qw":"7","sd":"90","sj":"2018-05-18 02:00:00"},{"tq":"多云","numtq":"01","qw":"6","sd":"90","sj":"2018-05-18 03:00:00"},{"tq":"多云","numtq":"01","qw":"6","sd":"90","sj":"2018-05-18 04:00:00"},{"tq":"阴","numtq":"02","qw":"6","sd":"90","sj":"2018-05-18 05:00:00"},{"tq":"阴","numtq":"02","qw":"6","sd":"90","sj":"2018-05-18 06:00:00"},{"tq":"阴","numtq":"02","qw":"6","sd":"90","sj":"2018-05-18 07:00:00"},{"tq":"阴","numtq":"02","qw":"6","sd":"90","sj":"2018-05-18 08:00:00"},{"tq":"阴","numtq":"02","qw":"7","sd":"75","sj":"2018-05-18 09:00:00"},{"tq":"阴","numtq":"02","qw":"8","sd":"60","sj":"2018-05-18 10:00:00"},{"tq":"阴","numtq":"02","qw":"8","sd":"60","sj":"2018-05-18 11:00:00"}]}}

你可能感兴趣的:(Java,天气API)