我有一个问题,我似乎无法弄清楚我的WindowsPhone 8.1应用程序。在我的客户端,我正在编写代码来从我查询的数据库中检索值。虽然我能够成功地使用我的Bars实体,但我似乎无法让我的团队实体工作。奇怪的是,它似乎已经过去并且卡在我的HttpResponseMessage对象的创建上。这是代码:
private void refresh_Click(object sender, RoutedEventArgs e)
{
GetTeams();
//getTeamById(10);
}
public async void GetTeams()
{
using (var client = new HttpClient())
{
teamsListBox.Items.Add("using block entered");
client.BaseAddress = new Uri("http://nflff.azurewebsites.net");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
teamsListBox.Items.Add("client's defaultrequestheaders done");
HttpResponseMessage response = await client.GetAsync("api/bars");//not getting past here
teamsListBox.Items.Add("right after response"); //not printing
if (response.IsSuccessStatusCode)
{
teamsListBox.Items.Add("if entered");
//IList<Teams> Teams = await response.Content.ReadAsAsync<IList<Teams>>();
string teams = await response.Content.ReadAsStringAsync();
int start = 0;
while (start != -1)
{
start = stringToTeam(teams, start); //throwing exception inside stringToTeams, which isn't being called anywhere else
}
}
teamsListBox.Items.Add(Teams.TeamsList.Count);
foreach (var team in Teams.TeamsList)
{
teamsListBox.Items.Add(team.ToString());
}
}
}
public int stringToTeam(String Team, int start)
{
start = Team.IndexOf("TeamID", start) + 8;
int end = Team.IndexOf(",", start); //start index correct
//teamsListBox.Items.Add(end);
int id = Convert.ToInt32(Team.Substring(start, end - start)); //throws FormatException for string to DateTime
//String id = Team.Substring(start, (end - start));
teamsListBox.Items.Add("id is " + id);
start = Team.IndexOf("TeamName", start) + 11;
end = Team.IndexOf("\"", start);
//teamsListBox.Items.Add(end);
String name = Team.Substring(start, (end - start));
teamsListBox.Items.Add("name is " + name);
String city = Team.Substring(start, (end - start));
teamsListBox.Items.Add("city is " + city);
start = Team.IndexOf("TeamState", start) + 12;
end = Team.IndexOf("\"", start);
//teamsListBox.Items.Add(end);
//creates a Teams model obj with values pulled from string and adds it to static list of objs in Teams model
//teamsListBox.Items.Add(Teams.TeamsList.Count);
Teams newTeam = new Teams(id, name, city);
Teams.TeamsList.Add(newTeam);
//teamsListBox.Items.Add(Teams.TeamsList.Count);
//teamsListBox.Items.Add(newTeam.ToString());
end += 4;
if (end <= Team.Length)
{
return end;
}
else
{
return -1;
}
}
当我运行此代码时,“响应后”不会打印到UI,而上面的测试消息是。此外,模拟器最终会破坏并从stringToTeam中的一行(我将发布另一个问题)中抛出一个FormatException,这在其他任何地方都没有被调用。还值得一提的是,我的Bars实体具有完全相同的代码(当然切换名称),并且条形图完全正常。我已经比较了这些方法中的每一行代码以及模型和服务器端控制器,它们完全相同。那么为什么条形码代码可以工作而团队代码却没有呢?
如果有帮助,这是我的型号代码:
public class Teams
{
private int teamID { get; set; }
private string teamName { get; set; }
private string teamCity { get; set; }
//private object teamFlag { get; set; } //Janci, I was never able to see your comments. What is teamFlag? Is that their logo?
public static List<Teams> TeamsList = new List<Teams>();
public Teams(int id, string name, string city)
{
teamID = id;
teamName = name;
teamCity = city;
}
public override string ToString()
{
return "Team: " + teamID.ToString() + " " + teamName.ToString() + " City: " + teamCity;
}
}
我在Azure网站上托管了一个SQL数据库,我在后端使用.NET。请帮我弄清楚为什么我的团队代码不起作用。感谢。
答案 0 :(得分:0)
我认为问题在于:
HttpResponseMessage response = await client.GetAsync("api/bars");
不应该是:
HttpResponseMessage response = await client.GetAsync("api/teams");
答案 1 :(得分:0)
问题结果是我在stringToTeam方法中有错误的开始和结束索引。一旦我修复了它,代码就可以正常工作和显示。方法的IndexOf和Substring调用应如下所示:
start = Team.IndexOf("TeamID", start) + 8;
int end = Team.IndexOf(",", start); //start index correct
//teamsListBox.Items.Add(end);
int id = Convert.ToInt32(Team.Substring(start, end - start)); //throws FormatException for string to DateTime
//String id = Team.Substring(start, (end - start));
//teamsListBox.Items.Add("id is " + id);
start = Team.IndexOf("TeamName", start) + 11;
end = Team.IndexOf("\"", start);
//teamsListBox.Items.Add(end);
String name = Team.Substring(start, (end - start));
//teamsListBox.Items.Add("name is " + name);
start = Team.IndexOf("TeamCity", start) + 11;
end = Team.IndexOf("\"", start);
String city = Team.Substring(start, (end - start));
在对我自己的反序列化器进行手工编码并为每个实体调整它之后,我发现了JsonConvert类。我强烈建议您不要编写自己的解串器。使用JsonConvert,我可以删除我的stringToTeam()方法并用以下内容替换旧的GetTeams():
public async void GetTeams()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://nflff.azurewebsites.net");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("api/teams");//not getting past here
if (response.IsSuccessStatusCode)
{
string s = await response.Content.ReadAsStringAsync();
var deserializedResponse = JsonConvert.DeserializeObject<List<Teams>>(s);
foreach (Teams team in deserializedResponse)
{
teamsListBox.Items.Add(team.ToString());
}
}
foreach (var team in Teams.TeamsList)
{
teamsListBox.Items.Add(team.ToString());
}
}
}
这种方法不那么乏味。