从网页读取JSON首先会引发错误,但之后会工作

时间:2014-01-14 10:17:03

标签: c# .net json windows-store-apps

我有一个应用程序从网页获取JSON字符串并解析它等等。我遇到的问题是应用程序的第一次运行会引发我这个错误:

A first chance exception of type
'System.Net.Http.HttpRequestException' occurred in mscorlib.dll An
error occurred while sending the request. System.Net.WebException:
Unable to connect to the remote server --->
System.Net.Sockets.SocketException: A connection attempt failed
because the connected party did not properly respond after a period of
time, or established connection failed because connected host has
failed to respond 128.0.46.69:80
    at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
    at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,
Socket s4, Socket s6, Socket& socket, IPAddress& address,
ConnectSocketState state, IAsyncResult asyncResult, Exception&
exception)
    --- End of inner exception stack trace ---
    at
System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)   
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult
ar)

之后,如果我从开始菜单启动它,它的效果很好。 我不知道为什么它第一次不能工作但是在它开始工作之后。

有什么想法吗?非常感谢

以下是一些代码:

private async Task GetSampleDataAsync()
        {
            if (this._groups.Count != 0) return;
            int loopNo = 0;
            while (true)
            {
                loopNo++;
                try
                {
                    // Fetchibg JSON file from http
                    var client = new HttpClient();
                    HttpResponseMessage response = await client.GetAsync(new Uri("http://etc/data.php"));

                    var jsonString = await response.Content.ReadAsStringAsync();
                    JsonObject jsonObject = JsonObject.Parse(jsonString);
                    JsonArray jsonArray = jsonObject["Groups"].GetArray();

                    foreach (JsonValue groupValue in jsonArray)
                    {
                        JsonObject groupObject = groupValue.GetObject();
                        SampleDataGroup group = new SampleDataGroup(groupObject["UniqueId"].GetString(),
                                                                    groupObject["Title"].GetString(),
                                                                    groupObject["Subtitle"].GetString(),
                                                                    groupObject["ImagePath"].GetString());

                        foreach (JsonValue itemValue in groupObject["Items"].GetArray())
                        {
                            JsonObject itemObject = itemValue.GetObject();
                            group.Items.Add(new SampleDataItem(itemObject["UniqueId"].GetString(),
                                                               itemObject["Title"].GetString(),
                                                               itemObject["Subtitle"].GetString(),
                                                               itemObject["ImagePath"].GetString(),
                                                               itemObject["Content"].GetString()));
                        }
                        this.Groups.Add(group);
                    }
                    break;
                }
                catch (Exception exp)
                {
                    Debug.WriteLine(exp.InnerException);
                }
            }
        }

我使用了分页模板。我也在MSDN上发布了这个问题,但我得到的唯一答案是来自主持人说我的网络或我试图访问的网页有问题。

我的网络和网页都没有任何问题。 它真的很奇怪,它从来没有第一次工作,但总是第二次。

0 个答案:

没有答案