Xamarin Forms的问题从webservices/rest api检索json的问题
问题描述:
我无法使用Xamarin表单中的PLC项目从webservice检索数据。 我已经尝试WebRequest和HttpClient,但得到各种例外。我也曾尝试 从这个样本运行代码:Xamarin Forms的问题从webservices/rest api检索json的问题
-
ConnectFailure(网络无法访问)
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create ("http://localhost:3000/profile/contacts"); webRequest.UseDefaultCredentials = true; webRequest.Method = "GET"; webRequest.Accept = "application/json"; webRequest.ContentType = "application/json"; webRequest.BeginGetResponse ((ar) => { var request = (HttpWebRequest)ar.AsyncState; try { using (var response = (HttpWebResponse)request.EndGetResponse (ar)) { var s = response.GetResponseStream(); } } catch (Exception ex) { var xy = ex; } }, webRequest);
-
ConnectFailure(请求的地址是无效在这种情况下)
var client = new System.Net.Http.HttpClient(); client.BaseAddress = new Uri("http://localhost:3000/"); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = await client.GetStringAsync ("profile/contacts");
我也试着运行这个示例中的代码:https://github.com/conceptdev/xamarin-forms-samples/tree/master/HttpClient/HttpClientDemo 尝试发出请求时会引发namesresolutionfail异常。
var client = new System.Net.Http.HttpClient();
client.BaseAddress = new Uri("http://api.geonames.org/");
var response = await client.GetAsync("earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=bertt");
var earthquakesJson = response.Content.ReadAsStringAsync().Result;
var rootobject = JsonConvert.DeserializeObject<Rootobject>(earthquakesJson);
return rootobject.earthquakes;
我测试了我的rest api(用Node.Js编写)并且它正在按照预期返回数据。
您是使用iOS或Android进行测试的?如果是Android,您的应用是否启用了Internet权限? – Jason 2014-10-06 02:13:06
是的,我在我的清单文件中有以下条目: –
noobie
2014-10-06 03:53:20