【问题标题】:Refresh Webclient to get new data刷新 Webclient 以获取新数据
【发布时间】:2012-04-03 14:44:28
【问题描述】:

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

解决办法是什么?

   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); 

【问题讨论】:

    标签: json windows-phone-7 webclient


    【解决方案1】:

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

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

    【讨论】:

    • 当我在上面替换它时,它会抛出一个 formatexpection WebClient webClient = new WebClient(); string url = string.Format("http://www.URL.aspx?stopid={0}&amp;ts={1}", HttpUtility.UrlEncode(myvar) + DateTime.Now.Ticks); Uri uri = new Uri(url); webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted); webClient.OpenReadAsync(uri); 编辑 - 刚刚添加它
    • 你能把它放到主要问题中吗?它更容易格式化。
    • myVar 和 DAteTime.Now.Ticks 之间应该是逗号,格式为:string.Format("url?stopid={0}&ts={1}", HttpUtility.UrlEncode(myvar) , DateTime.Now.Ticks);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    • 2015-09-17
    相关资源
    最近更新 更多