zoukankan      html  css  js  c++  java
  • 控制台调用天气API例子

    第一步,新建控制台应用程序,然后新建类:WeatherReport:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class WeatherReport
        {
            public string date { get; set; }
    
            public long temp { get; set; }
    
            public string text { get; set; }
    
    
        }
    }
    

     第二步,要获取需要的城市的woeid(where on earch id),比如北京是:

    https://www.metaweather.com/api/location/search/?query=beijing
    

     

    第三步编写Program类:

     woeid在Program类中用的到,URL地址(可复制到浏览器中查看格式内容):

    https://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20woeid%20in%20(2151330%20)&format=json

    using Newtonsoft.Json.Linq;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri("https://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20woeid%20in%20(2151330%20)&format=json");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    
                GetWeather(client).Wait();  
            }
    
            static async Task GetWeather(HttpClient cons)
            {
                using (cons)
                {
                    HttpResponseMessage res = await cons.GetAsync("");
    
                    res.EnsureSuccessStatusCode();
                    if (res.IsSuccessStatusCode)
                    {
                        string weather = await res.Content.ReadAsStringAsync();
    
                        JObject jobj = JObject.Parse(weather);
                        JToken jToken = jobj.First;
                        string WeatherState = jToken.First["results"]["channel"]["item"]["condition"].ToString();
                        WeatherReport report = Newtonsoft.Json.JsonConvert.DeserializeObject<WeatherReport>(WeatherState);
                        Console.WriteLine("
    ");
    
                        Console.WriteLine("Weather Station: Beijing");
                        Console.WriteLine("Temperature Details");
                        Console.WriteLine("-----------------------------------------------------------");
                        Console.WriteLine("Temperature (in deg. C): " + (report.temp - 32) * 0.55);// Converted from Fahrenheit to Celsius  
                        Console.WriteLine("Weather State: " + report.text);
                        Console.WriteLine("Applicable Time: " + report.date);
                        Console.ReadLine();
                    }
                }
            }  
        }
    }
    

     运行结果:

  • 相关阅读:
    本月时间按天显示
    微信小程序----当前时间的时段选择器插件(今天、本周、本月、本季度、本年、自定义时段)
    vuex进行传值
    echart 自定义 formatter
    git 登录流程
    Java学习-反射
    mysql数据类型char、varchar、text的一些区别
    微信小程序踩坑记录
    Rancher、Helm、HelmFile
    句子迷 2015_01_10
  • 原文地址:https://www.cnblogs.com/iframe/p/8397434.html
Copyright © 2011-2022 走看看