我正在用PCL创建我的第一个xamarin项目。
我创建了带有Get Response的WebApi2并在我的本地服务器上发布,该工作正常并正确提供JSON响应
网址:http://test.propertytaxapi.com/api/ward
回复: [{ “ID”:1, “zone_id”:1, “ward_no”: “1”},{ “ID”:2 “zone_id”:1, “ward_no”: “2”},{ “ID”: 3, “zone_id”:1, “ward_no”: “3”},{ “ID”:4 “zone_id”:1, “ward_no”: “4”},{ “ID”:5 “zone_id”: 1, “ward_no”: “5”},{ “ID”:6, “zone_id”:1, “ward_no”: “6”}]
但是当我试图在我的Xamarin项目中实现它并将其称为。</ p>
public async Task<List<Ward>> GetData()
{
using (var client = new HttpClient())
{
try
{
client.BaseAddress = new Uri("http://test.propertytaxapi.com/api");
var response = await client.GetAsync("ward");
response.EnsureSuccessStatusCode();
var placesJson = response.Content.ReadAsStringAsync().Result;
Ward wardData = new Ward();
if (placesJson != "")
{
var wlists= JsonConvert.DeserializeObject<List<Ward>>(placesJson);
wardList = wlists.ToList();
}
}
catch (Exception)
{
return null;
}
}
return wardList;
}
我在这一行得到例外: -
var response = await client.GetAsync(“ward”);
例外:
11-02 14:11:29.968 E / mono(2954):
11-02 14:11:29.968 E / mono(2954):未处理的异常:
11-02 14:11:29.968 E / mono(2954):System.Net.Http.HttpRequestException:发送请求时发生错误---&gt; System.Net.WebException:错误:NameResolutionFailure
11-02 14:11:29.968 E / mono(2954):在&lt; 6c708cf596db438ebfc6b7e012659eee&gt;中的System.Net.HttpWebRequest.EndGetResponse(System.IAsyncResult asyncResult)[0x00058]:0
11-02 14:11:29.968 E / mono(2954):在System.Threading.Tasks.TaskFactory 1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func
2 [T,TResult] endFunction,System.Action 1[T] endAction, System.Threading.Tasks.Task
1 [TResult]承诺, System.Boolean requiresSynchronization)[0x0000f] in&lt; 896ad1d315ca4ba7b117efb8dacaedcf&gt ;:0
11-02 14:11:29.968 E / mono(2954):---从抛出异常的先前位置开始的堆栈跟踪结束---
11-02 14:11:29.968 E / mono(2954):在&lt; 896ad1d315ca4ba7b117efb8dacaedcf&gt;中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c]:0
11-02 14:11:29.968 E / mono(2954):在&lt; 896ad1d315ca4ba7b117efb8dacaedcf&gt;中的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任务)[0x0003e]:0
11-02 14:11:29.968 E / mono(2954):在&lt; 896ad1d315ca4ba7b117efb8dacaedcf&gt;中的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task任务)[0x00028]:0
11-02 14:11:29.968 E / mono(2954):在&lt; 896ad1d315ca4ba7b117efb8dacaedcf&gt;中的System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(System.Threading.Tasks.Task任务)[0x00008]:0
11-02 14:11:29.968 E / mono(2954):at System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
11-02 14:11:29.968 E/mono ( 2954): at System.Net.Http.HttpClientHandler+<SendAsync>d__63.MoveNext () [0x00450] in <7736395a6c71409691a34accfc621095>:0
11-02 14:11:29.968 E/mono ( 2954): --- End of inner exception stack trace ---
11-02 14:11:29.976 E/mono-rt ( 2954): [ERROR] FATAL UNHANDLED EXCEPTION: System.Net.Http.HttpRequestException: An error occurred while sending the request ---> System.Net.WebException: Error: NameResolutionFailure
11-02 14:11:29.976 E/mono-rt ( 2954): at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x00058] in <6c708cf596db438ebfc6b7e012659eee>:0
11-02 14:11:29.976 E/mono-rt ( 2954): at System.Threading.Tasks.TaskFactory
1 [TResult] .FromAsyncCoreLogic(System.IAsyncResult iar,System.Func 2[T,TResult] endFunction, System.Action
1 [ T] endAction,System.Threading.Tasks.Task 1[TResult] promise, System.Boolean requiresSynchronization) [0x0000f] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
11-02 14:11:29.976 E/mono-rt ( 2954): --- End of stack trace from previous location where exception was thrown ---
11-02 14:11:29.976 E/mono-rt ( 2954): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
11-02 14:11:29.976 E/mono-rt ( 2954): at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
11-02 14:11:29.976 E/mono-rt ( 2954): at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
11-02 14:11:29.976 E/mono-rt ( 2954): at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
11-02 14:11:29.976 E/mono-rt ( 2954): at System.Runtime.CompilerServices.ConfiguredTaskAwaitable
1 + ConfiguredTaskAwaiter [TResult] .GetResult()[0x00000] in&lt; 896ad1d315ca4ba7b117efb8dacaedcf&gt ;:0
11-02 14:11:29.976 E / mono-rt(2954):在System.Net.Http.HttpClientHandler + d__63.MoveNext()[0x00450] in&lt; 7736395a6c71409691a34accfc621095&gt ;:0
11-02 14:11:29.976 E / mono-rt(2954):---内部异常堆栈跟踪结束---
请建议我获得回复的解决方案
答案 0 :(得分:1)
client.BaseAddress = new Uri(&#34; http://test.propertytaxapi.com/api/&#34;);
在基地址末尾添加反斜杠。试试上面的uri