Search code examples
c#windows-phone-8webclient

WebClient.DownloadStringAsync URL not working


I've got an issue about webclient on windowsphone 8. I've hit an API use webclient on my code.

    public void LoadData(bool refresh)
    {
        IsLoading = true;
        WebClient webclient = new WebClient();
        webclient.DownloadStringCompleted += webclient_DownloadStringCompleted;
        webclient.DownloadStringAsync(new Uri("http://api.xxxx.com/getQuestion"));
    }

    public void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        RootObject deserCustomers = JsonConvert.DeserializeObject<RootObject>(e.Result);
        foreach (var cust in deserCustomers.data)
        {
            Debug.WriteLine(cust.title);
            int length = cust.question.Length;
            string subquestion;
            if (length > 100) { subquestion = cust.question.Substring(0, 100); }
            else { subquestion = cust.question; }
            _questiondata.Add(new QuestionItem() { Title = cust.title, Question_Str = subquestion, Slug = cust.slug });
        }

        IsLoading = false;
    }

It's work for the first time and the data is exactly same as web. But when the web update with new question, and I try to call method LoadData for the second time, the data not same as web. the new update question on web does not appear in my apps (windows phone). What should I do, Is there any cache that make my result not updated like on web?

Thanks a lot.


Solution

  • I've got a help and it Solved. try this:

    public void LoadData(bool refresh)
    {
        IsLoading = true;
        WebClient webclient = new WebClient();
        webclient.DownloadStringCompleted += webclient_DownloadStringCompleted;
        Random random = new Random();
        webclient.DownloadStringAsync(new Uri("http://api.xxxx.com/getQuestion?random="+ random.Next().ToString()));
    }