刷新Webclient以获取新数据

时间:2012-04-03 14:44:29

标签: json windows-phone-7 webclient

在我使用它的那一刻,我必须重新启动应用程序才能刷新数据。有几个帖子说我应该在最后添加一个随机数字,但是我不能这样做,因为我把一个数字放在最后才能工作。

解决方案是什么?

   void myButton_Click(object sender, RoutedEventArgs e)
    {
        i = 0;
        lstService.Items.Clear();
        lstDestination.Items.Clear();
        listTime.Items.Clear();


        if (textSearchBox.Text == "Cranleigh Avenue")
        {
            textBlock2.Text = textSearchBox.Text;
            txtstopId.Text = "6445";
        }

        if (textSearchBox.Text == "Cranleigh Avenue (briamaw)")
        {
            textBlock2.Text = textSearchBox.Text;
            txtstopId.Text = "7099";
        }                

        else if (textSearchBox.Text == "Brighton Station (stop A)")
        {
            textBlock2.Text = textSearchBox.Text;
            txtstopId.Text = "7052";
        }

        else if (textSearchBox.Text == "Brighton Station (stop B)")
        {
            textBlock2.Text = textSearchBox.Text;
            txtstopId.Text = "7053";
        }


        else if (textSearchBox.Text == "Brighton Station (stop C)")
        {
            textBlock2.Text = textSearchBox.Text;
            txtstopId.Text = "7054";
        }


        else if (textSearchBox.Text == "Brighton Station (stop D)")
        {
            textBlock2.Text = textSearchBox.Text;
            txtstopId.Text = "7055";
        }

         else if (textSearchBox.Text == "Sea Life Centre (stop K)")
        {
            textBlock2.Text = textSearchBox.Text;
             txtstopId.Text = "7642";
        }

        else if (textSearchBox.Text == "Race Hill")
        {
            textBlock2.Text = textSearchBox.Text;
            txtstopId.Text = "6724";

        }



        string myvar = txtstopId.Text;
        textSearchBox.Text = "";
        try
        {
            WebClient webClient = new WebClient();
            Uri uri = new Uri("http://www.URL.aspx?stopid=" + HttpUtility.UrlEncode(myvar));
            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
            webClient.OpenReadAsync(uri);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    public int i;
    void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        DataContractJsonSerializer ser = null;



        try
        {


            ser = new DataContractJsonSerializer(typeof(RootContainer));
            RootContainer rootContainer = ser.ReadObject(e.Result) as RootContainer;
            foreach (Departures em in rootContainer.Departures)
            {
                i = i + 1;

                if (i < 9)
                {


                    string id = em.ServiceName;
                    string dt = em.Destination;
                    string tm = em.DepartureTimeAsString;
                    lstService.Items.Add(id);
                    lstDestination.Items.Add(dt);
                    listTime.Items.Add(tm);



                }
            }
        }


        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

修改

  WebClient webClient = new WebClient();
            string url = string.Format("http://www.URL.aspx?stopid={0}&ts={1}", HttpUtility.UrlEncode(myvar) + DateTime.Now.Ticks);                
            Uri uri = new Uri(url);
            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
            webClient.OpenReadAsync(uri); 

1 个答案:

答案 0 :(得分:3)

我会使用时间戳而不是随机数:

string url = string.Format("http://www.URL.aspx?stopid={0}&ts={1}",
      HttpUtility.UrlEncode(myvar),
      DateTime.Now.Ticks);
Uri uri = new Uri(url);