【发布时间】:2019-01-21 13:12:42
【问题描述】:
我正在使用 API (https://github.com/Hipo/university-domains-list-api) 编写小控制台程序,但我遇到了一个问题 - 当我第一次将信息写入变量 inputkeyword 和 inputcountry 时,它工作正常,但第二次等等时我得到与我第一次尝试相同的答案。感谢您的帮助。
public interface IRequestHandler
{
//Method to get the data of the repo provided by the url
string GetResult(string url);
}
// using request handler to get an url address
public static string GetResult(IRequestHandler requestHandler)
{
return requestHandler.GetResult(RequestConstants.Url);
}
public class RestSharpRequestHandler : IRequestHandler
{
public string GetResult(string url)
{
var client = new RestClient(url);
var response = client.Execute(new RestRequest());
return response.Content;
}
}
public class Input
{
public static string userInputKeyWord;
public static string userInputCountry;
public static void dot()
{
Console.WriteLine("Insert keyword which you need to find your university");
userInputKeyWord = Console.ReadLine();
Console.WriteLine("You entered keyword {0}", userInputKeyWord);
Console.WriteLine();
Console.WriteLine("Insert country which you need");
userInputCountry = Console.ReadLine();
Console.WriteLine("You entered country {0}", userInputCountry);
Console.WriteLine();
Console.WriteLine("Creating the list of schools according to your request.");
Console.WriteLine();
IRequestHandler restSharpRequestHandler = new RestSharpRequestHandler();
var response = GetResult(restSharpRequestHandler);
var Results = JsonConvert.DeserializeObject<List<School>>(response);
if (Results.Count() == 0)
{
Console.WriteLine("Nothing found.");
}
else
{
foreach (var release in Results)
{
Console.WriteLine("Country: {0}", release.Country);
Console.WriteLine("Name: {0}", release.Name);
Console.WriteLine("Domains: {0}", release.Domains);
Console.WriteLine();
}
}
}
}
// JSON properties
public class School
{
[JsonProperty("web_pages")]
public Uri[] WebPages { get; set; }
[JsonProperty("alpha_two_code")]
public string AlphaTwoCode { get; set; }
[JsonProperty("state-province")]
public object StateProvince { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("domains")]
public string[] Domains { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
//url for getting information from json
public class RequestConstants : Input
{
//public static string userInputKeyWord;
//public static string userInputCountry;
public string BaseUrl = "http://universities.hipolabs.com/";
public static string Url = "http://universities.hipolabs.com/search?name=" + userInputKeyWord + "&country=" + userInputCountry + "";
public string UserAgent = "User-Agent";
public string UserAgentValue = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
}
}
}
【问题讨论】:
-
这是您希望我们为您调试的大量代码。这是您开始熟悉调试的好时机。使用调试器,您可以在任何代码行上放置一个断点,以暂停该行的运行时执行。然后,您可以在代码执行时逐行逐步执行代码,并观察确切的运行时行为和变量值的变化。当你这样做时,你首先在哪一行观察到一个意想不到的结果?当时的价值观是什么?观察到的结果是什么?你期待什么结果?为什么?
-
观察到的结果是,每次我写关键字和国家时,我都会得到不同的结果——根据我的查询(url)列出的国家、名称和域。现在即使第二次尝试写关键字和国家,我也会得到相同的结果。
-
您错过了有关使用调试器和识别特定代码行和特定变量值的部分。这才是真正重要的部分。
-
我在公共静态字符串 Url = "universities.hipolabs.com/search?name=" + userInputKeyWord + "&country=" + userInputCountry + "";如果我不使用静态,我会在其他地方收到错误 return requestHandler.GetResult(RequestConstants.Url);
-
好吧,那行显示的代码甚至不应该编译。这些变量未在该范围内定义。至于您的问题,在这种情况下,“大多数问题”是什么?
Url的结果值是多少?您期望该值是多少?为什么?
标签: c# forms api input console