-7

我创建了一个名为telephoneNumber 的类和一个全局变量。该变量在一个方法中设置并在另一个方法中使用。但是,此变量返回 null。所有方法和这个全局变量在同一个类中。请帮助理解这个问题。非常感谢。我的课是:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Net.Http;
using Newtonsoft.Json;
using System.Collections;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string telephoneNumber;
        private async void GetSingleLocationInfo(string href)
        {

            var hereNetUrl = string.Format(
                href+"&accept=application/json"
                    );

            // get data from HERE.net REST API
            var httpClient = new HttpClient();
            var hereNetResponse = await httpClient.GetStringAsync(hereNetUrl);

            // deseralize JSON from Here.net 
            using (var tr = new StringReader(hereNetResponse))
            using (var jr = new JsonTextReader(tr))
            {
                var rootObjectResponse = new JsonSerializer().Deserialize<Object>(jr);

                String contacts = rootObjectResponse.ToString();
                int startIndex = contacts.IndexOf("phone");
                if (startIndex != -1)
                {
                    String value = contacts.Substring(startIndex, 50);
                    telephoneNumber=value.Substring(value.IndexOf("+"));
                }
                else
                {
                    telephoneNumber="";
                }

            }
        }
        private async void GeocodingWin8Query()
        {
            // build URL for Here.net REST service
            string currentgeoLoc = "37.75075,-122.393472";
            string queryString = "taxi";
            string appID = "dV04O71v5F3f2W"; // MAKE SURE TO GET YOUR OWN from developers.here.net
            object appCode = "8QVr5uSXwfcowDrA"; // MAKE SURE TO GET YOUR OWN from developers.here.net
            var hereNetUrl = string.Format(
                "http://demo.places.nlp.nokia.com/places/v1/discover/search?at={0}&q={1}&app_id={2}&app_code={3}&accept=application/json",
                    currentgeoLoc, queryString, appID, appCode);

            // get data from HERE.net REST API
            var httpClient = new HttpClient();
            var hereNetResponse = await httpClient.GetStringAsync(hereNetUrl);

            // deseralize JSON from Here.net 
            using (var tr = new StringReader(hereNetResponse))
            using (var jr = new JsonTextReader(tr))
            {
                var rootObjectResponse = new JsonSerializer().Deserialize<RootObject>(jr);


                List<Item> items=rootObjectResponse.results.items;


                foreach(Item item in items){
                    string href = item.href;
                    GetSingleLocationInfo(href);
                   Console.WriteLine (telephoneNumber);//returns null
                }


            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GeocodingWin8Query();
        }
    }
    public class Category
    {
        public string id { get; set; }
        public string title { get; set; }
        public string href { get; set; }
        public string type { get; set; }
    }

    public class Item
    {
        public List<double> position { get; set; }
        public int distance { get; set; }
        public string title { get; set; }
        public Category category { get; set; }
        public string icon { get; set; }
        public string vicinity { get; set; }
        public List<object> having { get; set; }
        public string type { get; set; }
        public string href { get; set; }
        public string id { get; set; }
        public double? averageRating { get; set; }
    }


    public class Context
    {
        public Location location { get; set; }
        public string type { get; set; }
    }

    public class Search
    {
        public Context context { get; set; }
    }

    public class RootObject
    {
        public Results results { get; set; }
        public Search search { get; set; }
    }
}
4

4 回答 4

3

因此,在您调用 GetSingleLocationInfo 的地方,您正在调用异步方法。因此,GetSingleLocationInfo cal 将一直运行到 await 语句,然后在它之前直接返回给调用者 httpClient.GetStringAsync(hereNetUrl); 已经回来了。

要解决此问题,您需要先等待调用 GetSingleLocationInfo,然后再尝试访问该变量。

于 2013-04-07T15:24:20.480 回答
1

由于GetSingleLocationInfo是异步的,它将被异步调用,因此Console.WriteLine (telephoneNumber); 将在GetSingleLocationInfo更改它之前调用。我认为您应该在调用该方法时等待。

于 2013-04-07T15:26:17.467 回答
0

找不到字符串时,String.Substring 返回 NULL。

于 2013-04-07T15:23:23.913 回答
0

它将“返回” null 很简单,因为telephoneNumber尚未设置。

您对变量的声明private string telephoneNumber;未设置任何值,因此它是空字符串或 null。

我的猜测是在您实际设置telephoneNumber为具有值的方法之前调用您打印出来的方法。

于 2013-04-07T15:24:26.087 回答