我写的是uwp app。
我尝试下载json并显示数据。
我发送方法lan和lon变量,把它和链接并下载数据。
位置代码:
public class LocationManager
{
public async static Task<Geoposition> GetPosition()
{
var accessStatus = await Geolocator.RequestAccessAsync();
if (accessStatus != GeolocationAccessStatus.Allowed) throw new Exception();
var geolocator = new Geolocator { DesiredAccuracyInMeters = 0 };
var position = await geolocator.GetGeopositionAsync();
return position;
}
}
下载JSON代码:
private const string APPID = "f3c45b5a19426de9ea6ba7eb6c6969d7";
public async static Task<RootObject> GetWeather(double lat, double lon)
{
var http = new HttpClient();
var url = String.Format(
"http://api.openweathermap.org/data/2.5/weather?lat={0}&lon={1}&units=metric&APPID="+APPID, lat, lon);
var response = await http.GetAsync(url);
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;
}
}
[DataContract]
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 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 Rain
{
[DataMember]
public double __invalid_name__1h { 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 Rain rain { 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; }
}
在那里我显示这样的数据:
public async void WeatherDisplay()
{
try
{
//var position = await LocationManager.GetPosition();
var geoLocator = new Geolocator();
geoLocator.DesiredAccuracy = PositionAccuracy.High;
Geoposition pos = await geoLocator.GetGeopositionAsync();
var lat = pos.Coordinate.Point.Position.Latitude;
var lon = pos.Coordinate.Point.Position.Longitude;
RootObject myWeather =
await OpenWeatherMapProxy.GetWeather(
lat,
lon);
Temperature.Text = ((int)myWeather.main.temp).ToString() + "°C";
// DescriptionTextBlock.Text = myWeather.weather[0].description;
City.Text = myWeather.name;
}
catch
{
}
}
我遇到延误问题,点按按钮,它会在30-40秒内显示数据。随着互联网一切都好。为什么我有这么大的延误?