1

I am having trouble retrieve data from a webservice using a PLC project in Xamarin forms. I have tried both the WebRequest and HttpClient but get various exceptions. I have also tried to run the code from this sample:

  1. ConnectFailure (Network is unreachable)

    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);
    
  2. ConnectFailure (The requested address is not valid in this context)

                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");
    

I have also tried to run the code from this sample: https://github.com/conceptdev/xamarin-forms-samples/tree/master/HttpClient/HttpClientDemo Which throws a namesresolutionfail exception when trying to make a request.

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;

I have tested my rest api (written in Node.Js) and it is returning data as expected.

2
  • Are you testing with iOS or Android? If Android, does your app have Internet Permissions enabled? Commented Oct 6, 2014 at 2:13
  • yes, I have the following entry in my manifest file: <uses-permission android:name="android.permission.INTERNET" /> </manifest> Commented Oct 6, 2014 at 3:53

1 Answer 1

2

Eee, what is localhost to Android? It doesn't know that localhost is actually your computer. As per api.geonames.org, that might be an emulator issue. Where are you testing your app? Try real device.

Sign up to request clarification or add additional context in comments.

2 Comments

When installing api.geonames.org on my phone it works, so probably emulator related. Will check this out thanks.
Silly mistake! I created an entry in the host file to map the local host address indirectly. I am getting the naming resolution error now, but as you mentioned is probably emulator related.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.