C# 尝试获取我的数据库信息但未成功
我使用localhost作为后端,它看起来像这样:C# 尝试获取我的数据库信息但未成功,c#,php,xamarin,mysqli,xamarin.forms,C#,Php,Xamarin,Mysqli,Xamarin.forms,我使用localhost作为后端,它看起来像这样: var createResult = await phpApi.getOurMainInfo (); foreach (var currentItem in createResult) { ourList.Add (new Contact () { ID = currentItem ["ID"].ToString (),
var createResult = await phpApi.getOurMainInfo ();
foreach (var currentItem in createResult) {
ourList.Add (new Contact ()
{
ID = currentItem ["ID"].ToString (),
Name = currentItem ["Name"].ToString (),
Number = currentItem ["Number"].ToString ()
});
};
在我的课堂上,我通过类似这样的httprequest获得它:
static public async Task<JArray> getOurMainInfo()
{
var httpClientRequest = new HttpClient ();
var result = await httpClientRequest.GetAsync ("http://localhost/GetMyLocal.php");
var resultString = await result.Content.ReadAsStringAsync ();
var jsonResult = JArray.Parse (resultString);
System.Diagnostics.Debug.WriteLine (jsonResult); //I get all the results here in my log.
return jsonResult;
}
当我尝试循环并获取信息时,代码如下所示:
var createResult = await phpApi.getOurMainInfo ();
foreach (var currentItem in createResult) {
ourList.Add (new Contact ()
{
ID = currentItem ["ID"].ToString (),
Name = currentItem ["Name"].ToString (),
Number = currentItem ["Number"].ToString ()
});
};
在我的xaml中,以获取示例名称:
<Label Text = "{Binding Name}"/>
我得到这个错误:
访问的JArray值的键值“Name”无效。应为Int32数组索引`
如果在手机上运行应用程序,localhost就是您的手机。我猜,服务器不会在你的手机上运行,对吧?但是你的手机会发送请求。或者您正在您的计算机上运行应用程序?在我的计算机上运行应用程序是当我在日志中尝试此操作时:“System.Diagnostics.Debug.WriteLine(jsonResult[“Name”]);”我收到此错误:“访问的JArray值具有无效键值:“Name”。应为Int32数组索引。”也许您可以使用JObject代替JArray。这只是一种预感。如果在你的手机上运行应用程序,localhost就是你的手机。我猜,服务器不会在你的手机上运行,对吧?但是你的手机会发送请求。或者您正在您的计算机上运行应用程序?在我的计算机上运行应用程序是当我在日志中尝试此操作时:“System.Diagnostics.Debug.WriteLine(jsonResult[“Name”]);”我收到此错误:“访问的JArray值具有无效键值:“Name”。应为Int32数组索引。”也许您可以使用JObject代替JArray。这只是一种预感。
System.Diagnostics.Debug.WriteLine (jsonResult["Name"]);