Forum Xamarin.Android

Xamarin Android HttpClient Not Working

Key1Key1 USMember

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

Posts

  • Key1Key1 USMember

    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

  • ainsophicalainsophical USMember ✭✭

    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.

  • Key1Key1 USMember

    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

  • 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);
        }
    
    }
    
  • improwiseimprowise Member ✭✭

    @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"?

  • MizanurRahman.0834MizanurRahman.0834 USMember ✭✭
    edited March 2018

    Just avoid the return statement
    OR Replace the void by StringType

    Hope this will help you

  • improwiseimprowise Member ✭✭

    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...

  • JoerlynMorfeJoerlynMorfe USMember ✭✭

    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

  • ateeqpashaateeqpasha USMember

    @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!

Sign In or Register to comment.