C#未从本地主机网页接收JSON字符串
我正在尝试使用xamarin for android和c#从本地主机web服务器上的网页获取JSON数据。网页根据URL查询字符串中的值运行SQL查询,并获取所有必要的数据 我用来获取JSON的urlC#未从本地主机网页接收JSON字符串,c#,json,xamarin,C#,Json,Xamarin,我正在尝试使用xamarin for android和c#从本地主机web服务器上的网页获取JSON数据。网页根据URL查询字符串中的值运行SQL查询,并获取所有必要的数据 我用来获取JSON的url http://localhost/api/index.php?search=testo 返回的JSON结果 {"name":"testo","description":"testo","price":"2","quantity":"3","dateadded":"2018-04-10"} 下面
http://localhost/api/index.php?search=testo
返回的JSON结果
{"name":"testo","description":"testo","price":"2","quantity":"3","dateadded":"2018-04-10"}
下面的代码是我在onCreate中调用以执行的方法
private void getResponse()
{
output.Text = "You have searched for: " + "'" + search + "'";
try
{
//the value of 'search' is the product they searched for
WebClient c = new WebClient();
var data = c.DownloadString("http://localhost/api/index.php?search=" + search);
JObject o = JObject.Parse(data);
nameField.Text = o["name"].ToString();
descrField.Text = o["description"].ToString();
priceField.Text = o["price"].ToString();
quantityField.Text = o["quantity"].ToString();
dateField.Text = o["dateadded"].ToString();
}
catch
{
nameField.Text = "Product not found.";
descrField.Text = "Product not found.";
priceField.Text = "Product not found.";
quantityField.Text = "Product not found.";
dateField.Text = "Product not found.";
}
}
该应用程序当前的反应是没有找到该产品(当它确实存在时),我认为它根本没有收集JSON,这可以解释它不起作用-如果你们有任何想法,我将不胜感激
谢谢原来问题出在HTML标签上,它试图解析HTML元素,如HTML、head、body等,但这不起作用!只需从索引文件中删除它,就可以了。感谢@Jason提出的测试结果的想法。在catch块上放置一个
(异常示例)
,然后调试它以查看您的错误getting@mituw16System.Net.WebException:错误:连接失败(连接被拒绝)--->System.Net.Sockets.SocketException:在System.Net.Sockets.Socket.Connect上拒绝连接(System.Net.EndPoint remoteEP)[0x000b6]in:0 at System.Net.WebConnection.Connect(System.Net.HttpWebRequest request)[0x0016d]in:0---内部异常堆栈跟踪结束---如果有允许连接的方法,我正在使用wamp。直接使用ip地址如何?@frz3993不幸出现相同错误(