我目前正在开发Weather API,我正在尝试从OpenWeatherMap.org获取值。但是当我点击按钮从网站上检索信息时。它给了我以下错误。Here's the Screenshot of the exception,I am Getting
此外,我点击按钮的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();
}
}
}
答案 0 :(得分: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());
}
}
答案 1 :(得分:-1)
((int)myWeather.main.temp).ToString()
我要问:为什么要int然后再回到字符串?
无论如何,问题的答案是:
你需要检查myWeather!= null&amp;&amp; myWeather.main!= null&amp;&amp; myWeather.main.temp!= null
其中一个是null,它给你例外。