我创建了一个名为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; }
}
}