异步操作未完成

时间:2013-11-27 23:13:00

标签: c# json parsing windows-phone-7

我有问题。我的代码没有返回纬度和经度来确定我的全局变量。

在调试时我注意到这一行:

webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);

我注意到这行不起作用,调试器似乎跳了起来,最终没有得到我需要的方法,不知道为什么会这样,早些时候它正常工作。

   private void GetDestination()
            {
                if (txtSearchTo.Text != String.Empty)
                {
                    string url = String.Format("http://maps.googleapis.com/maps/api/geocode/json?address={0}&sensor=false", txtSearchTo.Text);
                    try
                    {
                        WebClient webClient = new WebClient();                    
                        webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
                        webClient.DownloadStringAsync(new Uri(url));
                    }
                    catch
                    {
                        ///todo: criar if para verificar conexão   
                        MessageBox.Show("Verify your connection and try again!");
                    }
                }
                else
                {
                    txtSearchTo.Focus();
                }
            }


            private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
                if (e.Error != null)
                {
                    MessageBox.Show("Verify your connection and try again!");
                }
                else
                {
                    // Save the feed into the State property in case the application is tombstoned. 
                    this.State["json"] = e.Result;
                    destination = ParserJSON(e.Result);
                }
            }


private GeoCoordinate ParserJSON(string pJSON)
        {   
            //Se o JSON está presente
            if (pJSON != null)
            {
                try
                {
                    //Faz a conversão (parse) para um tipo jObject
                    JObject jObj = JObject.Parse(pJSON);

                    //Le o objeto da lista inteira
                    JArray results = jObj["results"] as JArray;

                    JToken firstResult = results.First;
                    JToken location = firstResult["geometry"]["location"];

                    GeoCoordinate coord = new GeoCoordinate()
                    {
                        Latitude = Convert.ToDouble(location["lat"].ToString()),
                        Longitude = Convert.ToDouble(location["lng"].ToString())
                    };
                    return coord;
                }
                catch 
                {
                    ///todo: if para verificar conexão                    
                    MessageBox.Show("Verify your connection and try again!");
                }                
            }
            return null;
        }

我听说API中的某些内容可能会限制我,但我检查了Url和JSon,你可以看到代码HERE生成的带有JSON的Url。

0 个答案:

没有答案
相关问题