我的應用程序可以很好地與WiFi工作,但是當它通過3G連接時,它會崩潰。HTTPClient在測量連接上失敗並且無線上工作正常
這裏是代碼:
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
if (!App.ViewModel.IsDataLoaded)
{
await App.ViewModel.LoadDataAsync();
DataContext = App.ViewModel;
}
}
代碼LoadDataAsync
public async Task<bool> LoadDataAsync()
{
Uri uri = new Uri("http://www.28cinema.az/eng/");
HttpClient client = new HttpClient();
HtmlDocument htmlDocument = new HtmlDocument();
HtmlNode htmlNode = new HtmlNode(0, htmlDocument, 1);
MovieGroup data = new MovieGroup();
string HtmlResult;
HtmlResult = await client.GetStringAsync(uri);
data.Title = "Today";
htmlDocument.LoadHtml(HtmlResult);
htmlNode = htmlDocument.DocumentNode.SelectSingleNode("//div[@class='poster_wrapper']");
for (var i = 0; i < (htmlNode.ChildNodes.Count); i++)
{
data.Items.Add(new MovieData
{
DetailsUrl = htmlNode.SelectSingleNode("div[" + (i + 1) + "]/div[1]/div[1]/a[1]").GetAttributeValue("href", ""),
Title = htmlNode.SelectSingleNode("div[" + (i + 1) + "]/div[1]/div[1]/a[1]").InnerText,
Poster = htmlNode.SelectSingleNode("div[" + (i + 1) + "]/img").GetAttributeValue("src", "No poster"),
Cinema = "28 Cinema"
});
}
Today = data;
IsDataLoaded = true;
return true;
}
異常消息:沒有網絡連接和任務被取消。
堆棧跟蹤:
at Cineman.ViewModels.MovieModel.<CreateTodayGroupAsync>d__4.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 Cineman.ViewModels.MovieModel.<LoadDataAsync>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 Cineman.MainPage.<OnNavigatedTo>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__0(Object state)
任何建議將不勝感激。
UPD:展望更遠透露斷點過程中的局部變量這一行:「htmlNode.InnerHtml」扔類型的異常「System.NullReferenceException」
有工作的3G連接。 –
例外說「無網絡連接」。它可能是一個權限問題? –
這是這裏的問題。有明確的互聯網連接。在WMappManifest IDCapNetworking中有一個通用權限,並進行檢查。 –