在我的visual studio解决方案中,我有2个项目:
我没有使用模拟器,我正在使用我的Android手机进行测试。
当我在浏览器的地址栏中输入http://localhost:55492/tables/todoitem
时,我会得到示例数据:
[
{"id":"784034d4-076c-4bbd-8ec0-72d3c1993c50","complete":false,"text":"First item"},
{"id":"ae90715d-063b-44b2-916e-97ca8082d436","complete":false,"text":"Second item"}
]
我在MainActivity中添加了这一行:
//...
Button button = FindViewById<Button>(Resource.Id.MyButton);
using (WebClient client = new WebClient())
{
byte[] arr = client.DownloadData("http://localhost:55492/tables/todoitem");
//...
}
我收到以下错误:未处理的异常。 System.Net.WebException:错误:ConnectFailure(拒绝连接)
是否有任何理由拒绝连接?
感谢您的帮助
答案 0 :(得分:2)
您需要在服务器URL中使用PC的IP地址,而不是localhost。使用localhost将导致手机尝试连接到自身。您可以通过打开控制台并在Windows上运行ipconfig以及在大多数其他平台上运行ifconfig来获取IP地址。