我有一种通过网络服务请求天气的方法。这在我的Lumia 520(在Windows Phone 8上)上运行得非常好,但Lumia 520(也运行Windows Phone 8)上的几个用户报告说它永远无法运行。我在beta测试中得到了一个,使beta输出成为异常,我被告知这是一个始终发生的TaskCanceledException异常。这是方法的代码:
public async Task<string> getWeatherForCurrentLocation( bool isViaSettings = true )
{
try
{
// Base url is to the web service with the API key and longitude/latitude provided.
var baseURL = string.Format(URL_FULL_WEATHER, APIKEY, this.Locations[0].Latitude, this.Locations[0].Longitude);
// Create an HttpClient instance
var httpClient = new HttpClient();
try
{
httpClient.Timeout = TimeSpan.FromSeconds(20);
// Send a request asynchronously continue when complete
HttpResponseMessage response = await httpClient.GetAsync(baseURL);
// Check that response was successful or throw exception
response.EnsureSuccessStatusCode();
// Read response asynchronously
string responseString = await response.Content.ReadAsStringAsync();
// Deserialise response into a new ResponseCity object
try
{
Location = JsonConvert.DeserializeObject<ResponseCity>(responseString);
// rest of code
}
catch (Exception e)
{
return "fail";
}
}
catch (TaskCanceledException t)
{
return "fail";
}
}
catch (HttpRequestException e)
{
return "fail";
}
return "success";
}
我已设置超时以确保在20秒内未成功返回天气,系统会显示错误提示。
有谁知道为什么会这样?
非常感谢任何指导!
更新:
我在测试版上遇到了一个有问题的Lumia 520用户,并且可以确认问题是网址是否为他返回了404。这是一个“https”网址。即使他试图通过IE打开链接,也没有任何显示。值得重复的是,这个URL绝对有效。服务器不是我自己的;它是forecast.io的。