Windows Phone 应用程序在 3G 下运行时会抛出异常(自动退出),但在 WIFI 下运行则正常。很奇怪

发布于 2024-12-20 21:59:35 字数 2486 浏览 1 评论 0原文

我已经尝试了数百次来查找这段代码的错误。 它只能通过WIFI工作,但是当我关闭手机上的WIFI并再次运行该应用程序时,该应用程序只是自动关闭,这意味着它抛出了异常。

该应用程序很简单,我使用 WebClint() 下载 HTML 源代码并使用 HTML Agility Pack 对其进行解析,然后将它们添加到列表中,foreach 该列表以创建每个新闻对象。

我尝试捕获异常堆栈跟踪并将其绑定到 texblock,它说一些 ArgumentOutOfRange 异常和 Genericlist(int32 索引)???

我不知道,wifi下没问题,但3G网络下不行。有人可以帮忙吗?

    public partial class MainPage : PhoneApplicationPage
{
    string srcHTML;
    HtmlNode UrlNode;
    ObservableCollection<News> newsList = new ObservableCollection<News>();
    List<HtmlNode> headlines;
    HtmlDocument hd;
    News n;
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        WebClient webClenet = new WebClient();
        webClenet.Encoding = new HtmlAgilityPack.Gb2312Encoding();
        webClenet.DownloadStringAsync(new Uri("http://www.6park.com/news/multi1.shtml", UriKind.RelativeOrAbsolute));
        webClenet.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClenet_DownloadStringCompleted);
    }


    private void webClenet_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        srcHTML = e.Result;
        GetHeadlinePage(srcHTML);
    }
    private void GetHeadlinePage(string srcHTML)
    {
        hd = new HtmlDocument();
        hd.LoadHtml(srcHTML);
        try
        {

            UrlNode = hd.DocumentNode.ChildNodes[1].ChildNodes[3].ChildNodes[8].ChildNodes["tr"].ChildNodes["td"].ChildNodes["ul"];
            headlines = UrlNode.Descendants("a").ToList();

            foreach (var headline in headlines)
            {

                if (headline.Attributes["href"].Value.Contains("6park"))
                {
                    n = new News();
                    n.NewsTitle = headline.InnerText;
                    n.NewsUrl = headline.Attributes["href"].Value;
                    n.NewsDetails = headline.NextSibling.InnerText.Replace("- ", "新闻来源:") + headline.NextSibling.NextSibling.InnerText + headline.NextSibling.NextSibling.NextSibling.InnerText;
                    newsList.Add(n);
                }
            }
        }

        catch (Exception ex)
        {
            //NewsSource.Text = ex.StackTrace + "\n" + ex.Message;
        }




        NewslistBox.ItemsSource = newsList;
        //NewsHeadlineWebBrowser.NavigateToString(ConvertExtendedASCII(headNews));
    }
}

I have tried hundreds of times to find errors for this piece of codes.
It only works through WIFI, but When I switch off WIFI on my phone, and run the app again, this app just shut down automatically, which means it thrown an exception.

The app is simple, I used WebClint() to download HTML source and parsed it with HTML Agility Pack, then added them to a list, foreach the list to creat each news object.

I have tried catch the exception stacktrace and bind it to a texblock, It said some of ArgumentOutOfRange exception and Genericlist(int32 index)???

I have no idea about it, It was fine in wifi, but not in 3G network. Can anyone help?

    public partial class MainPage : PhoneApplicationPage
{
    string srcHTML;
    HtmlNode UrlNode;
    ObservableCollection<News> newsList = new ObservableCollection<News>();
    List<HtmlNode> headlines;
    HtmlDocument hd;
    News n;
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        WebClient webClenet = new WebClient();
        webClenet.Encoding = new HtmlAgilityPack.Gb2312Encoding();
        webClenet.DownloadStringAsync(new Uri("http://www.6park.com/news/multi1.shtml", UriKind.RelativeOrAbsolute));
        webClenet.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClenet_DownloadStringCompleted);
    }


    private void webClenet_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        srcHTML = e.Result;
        GetHeadlinePage(srcHTML);
    }
    private void GetHeadlinePage(string srcHTML)
    {
        hd = new HtmlDocument();
        hd.LoadHtml(srcHTML);
        try
        {

            UrlNode = hd.DocumentNode.ChildNodes[1].ChildNodes[3].ChildNodes[8].ChildNodes["tr"].ChildNodes["td"].ChildNodes["ul"];
            headlines = UrlNode.Descendants("a").ToList();

            foreach (var headline in headlines)
            {

                if (headline.Attributes["href"].Value.Contains("6park"))
                {
                    n = new News();
                    n.NewsTitle = headline.InnerText;
                    n.NewsUrl = headline.Attributes["href"].Value;
                    n.NewsDetails = headline.NextSibling.InnerText.Replace("- ", "新闻来源:") + headline.NextSibling.NextSibling.InnerText + headline.NextSibling.NextSibling.NextSibling.InnerText;
                    newsList.Add(n);
                }
            }
        }

        catch (Exception ex)
        {
            //NewsSource.Text = ex.StackTrace + "\n" + ex.Message;
        }




        NewslistBox.ItemsSource = newsList;
        //NewsHeadlineWebBrowser.NavigateToString(ConvertExtendedASCII(headNews));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法少女 2024-12-27 21:59:35

我将调试传递给 GetHeadlinePage() 的值。

我怀疑响应因网络而异,或者请求超时,或者您收到其他错误。

我假设对 LoadHtml() 的调用失败,因为这不在任何异常处理/捕获中,并且您没有验证传递给它的值。

I'd debug the value passed to GetHeadlinePage().

I'd suspect that the response is different based on the network or the request is timing out or you're getting some other error.

I'd assume that the call to LoadHtml() is failing as this isn't inside any exception handling/trapping and you've not validating the value passed to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文