Abhishek Dey
Abhishek Dey

Reputation: 1639

JSON data not updating in Windows Phone 8

I'm using Newtonsoft JSON library to deserialize a JSON response from my web server. NOw, strangely I always receive same data although data is different as I have checked.

Code:

public Questions()
        {
            InitializeComponent();
            this.DataContext = App.ViewModel;
            WebClient wc = new WebClient();

            Uri request = new Uri("http://www.thestringsproject.com/q/json");
            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(CompletedDownload);
            wc.DownloadStringAsync(request);


        }

        private void CompletedDownload(object sender, DownloadStringCompletedEventArgs e)
        {

            var container = JsonConvert.DeserializeObject(e.Result) as JObject;

            List<JObject> result = container["cs"].Children()
                                .Cast<JObject>()

                                .ToList();
            foreach (JObject p in result)
            {
                var q = p["question"];
                questions.Add(q.ToString());
            }

            App.ViewModel.Items.Clear();
            if (questions.Count > 0)
            {
                App.ViewModel.Items.Clear();
                for (int i = 0; i < questions.Count; i++)
                {
                    App.ViewModel.Items.Add(new ItemViewModel { LineOne = questions[i], LineThree=(i+1).ToString() });
                }
            }

        }

Upvotes: 0

Views: 56

Answers (1)

Igor Kulman
Igor Kulman

Reputation: 16361

There are two things take come to mind. The first is that the WebClient caches the data, so try to add some random parameter to the url, like "http://www.thestringsproject.com/q/json?x="+DateTime.Now.Ticks and check the data you get back from the server.

The second one is that you get the new data back but App.ViewModel.Items is a simple List<T> instead of an ObservableCollection<T> so the data is the just the UI does not get updated.

Upvotes: 2

Related Questions