我正在使用httpclient getasync调用。而且我要打多次。 有时它会给出这种异常,而这种异常不会属于try catch块。
public async Task<(string exception, ObservableCollection<AddressInfo> autoCompleteInfo)> GetGooglePlacesAsync(string input)
{
try
{
using (var client = new HttpClient())
{
var response = await client.GetStringAsync(string.Format(GooglePlacesApi,
GooglePlacesApiKey, WebUtility.UrlEncode(input)));
PlacesLocationPredictions predictionList = JsonConvert.DeserializeObject<PlacesLocationPredictions>(response);
if (predictionList.Status == "OK")
{
LocationList.Clear();
if (predictionList.Predictions.Count > 0)
{
foreach (Prediction prediction in predictionList.Predictions)
{
LocationList.Add(new AddressInfo
{
Address = prediction.Description,
PlaceID = prediction.Place_Id
});
}
}
}
else
{
return (predictionList.Status, null);
}
}
}
catch (Exception ex)
{
return (ex.Message, null);
}
return (null, LocationList);
}