Forum Xamarin.Forms

API Post Call not calling

Hi,

In a Xamarin Form app, from the Master Page menu, in the Portable project, I call Content page InfoScreen into the Detail. InfoScreen code looks something like this:

    public InfoScreen()
    {
        InitializeComponent();
        ListAccounts();
    }

    public void ListAccounts()
    {
        SearchAccounts SA = new SearchAccounts();

        SearchAccountRequest searchAccountRequest = new SearchAccountRequest("000123456789", "","","","");
        var searchAccountResult = SA.SearchAccountAsync(searchAccountRequest);
    }

Notice that on load it calls ListAccounts, located in a class in the Portable project, which looks as follows:

    public async Task<SearchAccountResult> SearchAccountAsync(SearchAccountRequest searchAccountRequest)
    {

        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:00/api/");
        string jsonData = JsonConvert.SerializeObject(searchAccountRequest);
        var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
        var response = await client.PostAsync("SearchForAccount", content);
        var result = response.Content.ReadAsStringAsync().Result;

        if (result != "")
        {
            var sessionResponseJson = JsonConvert.DeserializeObject<SearchAccountResult>(result);
        }

However, when var response = await client.PostAsync("SearchForAccount", content); gets hit, it just goes back to InfoScreen and continues to load. The API is never hit, which I'm running locally on debug mode. I tested with Postman and it's working correctly.

I also tried:

        var client = new HttpClient();
        var data = JsonConvert.SerializeObject(searchAccountRequest);
        var content = new StringContent(data, Encoding.UTF8, "application/json");
        var response = await client.PostAsync("http://localhost:00/api/SearchForAccount", content);

        if (response.IsSuccessStatusCode)
        {
            var result = JsonConvert.DeserializeObject<SearchAccountResult>(response.Content.ReadAsStringAsync().Result);
        }
        else
        {
            //
        }

Same results. What am I missing?

Thanks!

Answers

  • RavinderJangraRavinderJangra USMember ✭✭✭

    if you are trying to run the code in emulator or device, your hosted api will not be reachable. so it will give you 404

  • RicardoMedinaRicardoMedina USMember ✭✭

    @RavinderJangra said:
    if you are trying to run the code in emulator or device, your hosted api will not be reachable. so it will give you 404

    Thanks for the response. Yes, I'm using the VisualStudio_android emulate. So, how do I debug this then?

    What I mean is, I need to test the code, get results backs to see the data displayed. If I can't use emulator to reach a hosted API, what's the alternative?

    Thanks!

  • RavinderJangraRavinderJangra USMember ✭✭✭

    you can host your api in you machine or in the local network or you can use 10.0.2.(2/...). you will have to check last digits. just try to call api in browser in the emulator.

  • RicardoMedinaRicardoMedina USMember ✭✭

    Hi,

    I tried calling the DEV box and local machine. But the only thing I'm getting is:

    Id = 1, Status = WaitingForActivation, Method = {null}

  • RicardoMedinaRicardoMedina USMember ✭✭

    BTW, locally I'm running it with VS in IIS Express - Virtual Directory. So I'm not sure it can translate that into an IP address.

  • RavinderJangraRavinderJangra USMember ✭✭✭

    @RicardoMedina did you tried that ip I told 10.0.2.(2/3/..) check it in browser if it works then go forward

  • RicardoMedinaRicardoMedina USMember ✭✭

    @RavinderJangra said:
    @RicardoMedina did you tried that ip I told 10.0.2.(2/3/..) check it in browser if it works then go forward

    Hi,

    That's not the issue, from the emulator's browser I can connect to the DEV server without an issue. I'm missing something on the code.

  • RicardoMedinaRicardoMedina USMember ✭✭

    @RavinderJangra said:
    @RicardoMedina did you tried that ip I told 10.0.2.(2/3/..) check it in browser if it works then go forward

    But to answer your question, from my local machine, as to be able to debug, I haven't been able to get it to work. I'm using VS to run the API and it runs on IIS Express. I can't figure how to get the local IP/Port to work, not even in Postman.

  • kizanlikkizanlik TRMember ✭✭
    @RicardoMedina

    in emulator, "localhost" means the emulator itself but i understand that the api is running on your dev pc.

    so, there are two options:
    1) changing "localhost/..." to "devpcename/..."
    2) changing "localhost/..." to "dev.pc.ip.add/..."

    because the api is running on iis express, you also need to give http port information. so, the urls above should be like this:

    dev.pc.ip.add:48000 <- iis express port

    if the app targets android devices, don't forget adding "internet" permission into androidmanifest file.

    and make sure that the pc which is the api is running must allow http network requests.
Sign In or Register to comment.