0

我目前正在研究 Weather API,我正在尝试从 OpenWeatherMap.org 获取值。但是当我单击按钮从网站检索信息时。它给了我以下错误。这是异常的屏幕截图,我正在获取

这也是我单击的按钮的 UI。 这是屏幕截图

System.Exception was unhandled by user code
HResult=-2146233088
Message=Exception of type 'System.Exception' was thrown.
Source=UWPWeather
StackTrace:
   at UWPWeather.LocationManager.<GetPosition>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at UWPWeather.MainPage.<Button_Click>d__1.MoveNext()
   InnerException: 

OpenWeatherMapProxy.cs 代码:

        using System;
        using System.Collections.Generic;
        using System.IO;
        using System.Linq;
        using System.Net.Http;
        using System.Runtime.Serialization;
        using System.Runtime.Serialization.Json;
        using System.Text;
        using System.Threading.Tasks;

        namespace WeatherAPI
        {
        public class OpenWeatherMapProxy
        {
        public async static Task<RootObject>GetWeather(double lat,double lon)

        {

        var http = new HttpClient();

        var response = await http.GetAsync("http://api.openweathermap.org/data/2.5/weather? lat=35&lon=77.20081&appid=b1b15e88fa797225412429c1c50c122a");


        var result = await response.Content.ReadAsStringAsync();

        var serializer = new DataContractJsonSerializer(typeof(RootObject));

        var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));

        var data = (RootObject)serializer.ReadObject(ms);
        return data;

    }

}

public class Coord
{
    [DataMember]
    public double lon { get; set; }
    [DataMember]
    public double lat { get; set; }
}
[DataContract]
public class Weather
{
    [DataMember]
    public int id { get; set; }
    [DataMember]
    public string main { get; set; }
    [DataMember]
    public string description { get; set; }
    [DataMember]
    public string icon { get; set; }
}
[DataContract]
public class Main
{
    [DataMember]
    public double temp { get; set; }
    [DataMember]
    public int pressure { get; set; }
    [DataMember]
    public int humidity { get; set; }
    [DataMember]
    public double temp_min { get; set; }
    [DataMember]
    public double temp_max { get; set; }
}
[DataContract]
public class Wind
{
    [DataMember]
    public double speed { get; set; }
    [DataMember]
    public int deg { get; set; }
}
[DataContract]
public class Clouds
{
    [DataMember]
    public int all { get; set; }
}
[DataContract]
public class Sys
{
    [DataMember]
    public int type { get; set; }
    [DataMember]
    public int id { get; set; }
    [DataMember]
    public double message { get; set; }
    [DataMember]
    public string country { get; set; }
    [DataMember]
    public int sunrise { get; set; }
    [DataMember]
    public int sunset { get; set; }
}
[DataContract]
public class RootObject
{
    [DataMember]
    public Coord coord { get; set; }
    [DataMember]
    public List<Weather> weather { get; set; }
    [DataMember]
    public string @base { get; set; }
    [DataMember]
    public Main main { get; set; }
    [DataMember]
    public Wind wind { get; set; }
    [DataMember]
    public Clouds clouds { get; set; }
    [DataMember]
    public int dt { get; set; }
    [DataMember]
    public Sys sys { get; set; }
    [DataMember]
    public int id { get; set; }
    [DataMember]
    public string name { get; set; }
    [DataMember]
    public int cod { get; set; }
}

MainPage.xaml.cs 代码:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;

    // The Blank Page item template is documented at           http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

   namespace WeatherAPI
   {
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        RootObject myWeather = await OpenWeatherMapProxy.GetWeather(20.0,30.0);
        ResultTextBlock.Text = myWeather.name + " - " + ((int)myWeather.main.temp).ToString();
    } 
}

}

4

2 回答 2

0

这是正在运行的代码(它正在运行一个控制台应用程序):

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class OpenWeatherMapProxy
    {
        public async static Task<RootObject> GetWeather(double lat, double lon)

        {

            var http = new HttpClient();

            var response = await http.GetAsync("http://api.openweathermap.org/data/2.5/weather?lat=35&lon=77.20081&appid=b1b15e88fa797225412429c1c50c122a");


            var result = await response.Content.ReadAsStringAsync();

            var serializer = new DataContractJsonSerializer(typeof(RootObject));

            var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));

            var data = (RootObject)serializer.ReadObject(ms);
            return data;

        }
    }


    public class Coord
    {
        [DataMember]
        public double lon { get; set; }
        [DataMember]
        public double lat { get; set; }
    }

    [DataContract]
    public class Weather
    {
        [DataMember]
        public double id { get; set; }
        [DataMember]
        public string main { get; set; }
        [DataMember]
        public string description { get; set; }
        [DataMember]
        public string icon { get; set; }
    }

    [DataContract]
    public class Main
    {
        [DataMember]
        public double temp { get; set; }
        [DataMember]
        public double pressure { get; set; }
        [DataMember]
        public double humidity { get; set; }
        [DataMember]
        public double temp_min { get; set; }
        [DataMember]
        public double temp_max { get; set; }
    }

    [DataContract]
    public class Wind
    {
        [DataMember]
        public double speed { get; set; }
        [DataMember]
        public double deg { get; set; }
    }

    [DataContract]
    public class Clouds
    {
        [DataMember]
        public double all { get; set; }
    }

    [DataContract]
    public class Sys
    {
        [DataMember]
        public double type { get; set; }
        [DataMember]
        public double id { get; set; }
        [DataMember]
        public double message { get; set; }
        [DataMember]
        public string country { get; set; }
        [DataMember]
        public double sunrise { get; set; }
        [DataMember]
        public double sunset { get; set; }
    }

    [DataContract]
    public class RootObject
    {
        [DataMember]
        public Coord coord { get; set; }
        [DataMember]
        public List<Weather> weather { get; set; }
        [DataMember]
        public string @base { get; set; }
        [DataMember]
        public Main main { get; set; }
        [DataMember]
        public Wind wind { get; set; }
        [DataMember]
        public Clouds clouds { get; set; }
        [DataMember]
        public double dt { get; set; }
        [DataMember]
        public Sys sys { get; set; }
        [DataMember]
        public double id { get; set; }
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public double cod { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            RootObject myWeather = Task.Run(() => OpenWeatherMapProxy.GetWeather(20.0, 30.0)).Result;
            Console.WriteLine( myWeather.name + " - " + ((int)myWeather.main.temp).ToString()); 
        }

    }
于 2016-03-21T18:02:57.977 回答
-1
((int)myWeather.main.temp).ToString()

我不得不问:为什么要 int 然后再回到 string ?

无论如何,您的问题的答案:

您需要检查 myWeather != null && myWeather.main != null && myWeather.main.temp != null

其中一个是空的,这给了你例外。

于 2016-03-21T17:34:40.033 回答