2014-02-07 31 views
0

我的應用程序可以很好地與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」

回答

0

那麼它解決了,當我重新啓動Visual Studio。奇怪的!

0

當你發現,如果試圖做一個HTTP請求,而有沒有網絡連接,它會拋出異常。

我建議您處理例外情況,例如使用try/catch。如何向用戶顯示異常詳細信息取決於您。

+0

有工作的3G連接。 –

+0

例外說「無網絡連接」。它可能是一個權限問題? –

+0

這是這裏的問題。有明確的互聯網連接。在WMappManifest IDCapNetworking中有一個通用權限,並進行檢查。 –

相關問題