Share via


Xamarin Android HttpClient Not Working

Question

Tuesday, August 8, 2017 5:14 AM

hi all I use this code for httpclient

    private async void Btn_Click(object sender, System.EventArgs e)
    {
        var uri = new Uri("http://www.pizzaboy.de/app/pizzaboy.json");
        HttpClient myClient = new HttpClient(new Xamarin.Android.Net.AndroidClientHandler());

        var response = await myClient.GetAsync(uri);
        if (response.IsSuccessStatusCode)
        {
            var content = await response.Content.ReadAsStringAsync();
            //var Items = JsonConvert.DeserializeObject<List<RootObject>>(content);
            Console.WriteLine("");
        }
    }

but when it wants myClient.GetAsync(uri) this project stop and show error unfortunately has stopped

All replies (11)

Tuesday, August 8, 2017 1:35 PM

this is a problem for Emulator visual studio in version Marshamallow in the Emulator KitKat no problem the best microsoft fix bug in lewd so develop in windows mobile :D


Saturday, August 12, 2017 6:39 PM

i am having the same problem lately. httpclient works on a physical device but I get absolutely no errors in debugging on vs emulator, it basically just skips over the http calls.


Sunday, August 13, 2017 6:18 AM

yes httpclient not work in protocol https but my problem was that in the emulator Marshamallow not connect to internet but emulator KitKat by default connect to internet that one emulator by default success setting network but the other emulator in the setup not connect some somewhat confusing


Tuesday, August 15, 2017 8:07 AM

Here is the code to access api using HttpClient. Hope, It will help you.

public class BasicSettingsService { private HttpClient client;

    public async void GetWorkPlaces(string status)
    {
    var content="";
        client = SecurityConstants.ConfigHeader(client);

        var uri = new Uri("http://www.pizzaboy.de/app/pizzaboy.json"); // Your url is here

        try
        {
            var response = await client.GetAsync(uri);
            if (response.IsSuccessStatusCode)
            {
                content = await response.Content.ReadAsStringAsync();                   
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return content ;
    }

private void ConfigHeader()
    {
        var authData = string.Format("{0}:{1}", SecurityConstants.Username, SecurityConstants.Password);
        var authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(authData));

        client = new HttpClient();
        client.MaxResponseContentBufferSize = 256000;
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue);
    }

}

Tuesday, March 20, 2018 1:44 PM

@"MizanurRahman.0834" said: Here is the code to access api using HttpClient. Hope, It will help you.

public class BasicSettingsService { private HttpClient client;

    public async void GetWorkPlaces(string status)
    {
  var content="";
        client = SecurityConstants.ConfigHeader(client);

        var uri = new Uri("http://www.pizzaboy.de/app/pizzaboy.json"); // Your url is here

        try
        {
            var response = await client.GetAsync(uri);
            if (response.IsSuccessStatusCode)
            {
                content = await response.Content.ReadAsStringAsync();                   
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return content ;
    }

private void ConfigHeader() { var authData = string.Format("{0}:{1}", SecurityConstants.Username, SecurityConstants.Password); var authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(authData));

        client = new HttpClient();
        client.MaxResponseContentBufferSize = 256000;
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue);
    }

}

Does this really work, just looking at the code (have not tried it) I see GetWorkPlaces being declared as void but still trying to return "return content"?


Tuesday, March 20, 2018 2:28 PM

Just avoid the return statement OR Replace the void by StringType

Hope this will help you


Tuesday, March 20, 2018 3:44 PM

For anyone that also has problem with REST, HttpClient etc on Xamarin with Android, it most likely is not the code that is wrong.

See this:

https://forums.xamarin.com/discussion/124041/actuall-working-sample-of-xamarin-form-that-does-rest-on-android

and this:

https://github.com/xamarin/xamarin-forms-samples/issues/300

I really wish that noone else has to go through our nightmare with Xamarin and give up before you can get this to work...


Saturday, May 19, 2018 11:32 AM

It's Simple try to change in the android Proyect>Propirties>Android Opcion>Advance>Http Client Implementations>Android.

I was a lot of Problem when I was called, the Second Issues was in the uri I called localHost, but local host reference android divice.

Please let me know is ok


Thursday, September 6, 2018 2:33 PM

It worked for me:

Proyect>Propirties>Android Opcion>Advance>

1- Http Client Implementations = Managed 2- SSL/TLS implementation = Native TLS 1.2 +

CODE:

var client = new HttpClient(new AndroidClientHandler());

var response = await _client.GetAsync(Url); var content = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject(content);

return result;

More information: blog.xamarin.com/securing-web-requests-with-tls-1-2


Tuesday, November 12, 2019 2:46 PM

@wbenay said: It worked for me:

Proyect>Propirties>Android Opcion>Advance>

1- Http Client Implementations = Managed 2- SSL/TLS implementation = Native TLS 1.2 +

CODE:

var client = new HttpClient(new AndroidClientHandler());

var response = await _client.GetAsync(Url); var content = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject(content);

return result;

More information: blog.xamarin.com/securing-web-requests-with-tls-1-2

Thanks, issue is resolved!


Tuesday, January 19, 2021 3:15 AM

@wbenay said: It worked for me:

Proyect>Propirties>Android Opcion>Advance>

1- Http Client Implementations = Managed 2- SSL/TLS implementation = Native TLS 1.2 +

CODE:

var client = new HttpClient(new AndroidClientHandler());

var response = await _client.GetAsync(Url); var content = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject(content);

return result;

More information: blog.xamarin.com/securing-web-requests-with-tls-1-2

How can I solve the same problem for xamarin ios? My url addresses are in the form of https: // .... The device works fine in my tests, but does not respond at all in testflight trials.