locked
Reading JSON from webpage throws error at first but works after RRS feed

  • Question

  • Hello

    I have an app that gets a JSON string from a webpage and parses it, etc. The problem I'm having is that the first run of the app throws me this error:

    After that, if I start it from the start menu it works great.

    I dont know why the first time it wont work but after it starts working.

    Any ideas? Thanks anticipated

    Here's some code:

    private async Task GetSampleDataAsync()
            {
                if (this._groups.Count != 0) return;
                int loopNo = 0;
                while (true)
                {
                    loopNo++;
                    //var msgLoop = new MessageDialog("loop " + loopNo.ToString());
                    //await msgLoop.ShowAsync();
                    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();
    
                        //JsonObject root = Windows.Data.Json.JsonValue.Parse(jsonString).GetObject();
                        //bool isSuccess = root.GetNamedString("status") == "ok";
                        //string type = root["type"].GetString();
                        //int size = (int)root.GetNamedNumber("size");
                        //JsonObject subObject = root.GetNamedObject("");
                        //JsonArray jsonArray = subObject["Groups"].GetArray();
                        //Uri dataUri = new Uri("ms-appx:///DataModel/SampleData.json");
                        //StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
                        //string jsonText = await FileIO.ReadTextAsync(file);
                        //JsonObject jsonObject = JsonObject.Parse(jsonText);
                        //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)
                    {
                        var msg = new MessageDialog(exp.Message + " ||| " + exp.InnerException);
                        msg.ShowAsync();
                    }
                }
            }

    I used the splitpage template. 

     


    If you don't gain anything from arguing with someone, do yourself a favor and stop wasting your time for free.


    • Edited by Andrei Pi Monday, January 13, 2014 10:00 AM
    Monday, January 13, 2014 9:59 AM

All replies