2

I'am trying to get data from my simple WebApi web service. In Android Xamarin application i've got very simple code:

public class Persons
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
}

class WebRequests
{
    public static List<Persons> getPersons()
    {
        List<Persons> lp = new List<Persons>();
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        try
        {
            HttpResponseMessage response = client.GetAsync("http://localhost:49814/api/Data/Persons").Result;
            if (response.IsSuccessStatusCode)
            {
                lp = JsonConvert.DeserializeObject<Persons[]>(response.Content.ReadAsStringAsync().Result).ToList();
            }
        }
        catch
        {

        }

        return lp;
    }
}

Problem is with line

HttpResponseMessage response = client.GetAsync("http://localhost:49814/api/Data/Persons").Result;

Because i'am receiving unhandler error. I don't know whats is wrong, because there is no any error message i catch statement. I was testing this code in a simple Win Forms application and everything was fine. For testing Xamarin app I'am using build-in emulator. Maybe should I use a real device for testing?

2
  • Since you're using localhost you must use it on the same machine. If you're going to use a real device (which I prefer) you need to setup a proper host name or IP. Commented Jun 16, 2016 at 6:50
  • I know that. Web service, and android emulator are running on the same machine. Commented Jun 16, 2016 at 6:52

1 Answer 1

1

Do not use localhost, because localhost is pointing to 127.0.0.1 of your android device, if you want work with your web api, set up IIS server or publish your web app somewhere, I hope it will help you.

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

2 Comments

I've fixed that problem a long time ago, but Your answer is correct, that was a problem ;)
Thank you for your answer. Here's another useful answer -> stackoverflow.com/questions/42365046/…

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.