App freezing when performing HttpWebRequest

Hey there,

I am pretty new to Xamarin and I am loving it!
In my application I perform a HttpWebRequest, but when the request is busy the app freezes.

I am pretty sure I need to use "AsyncTask" but what I tried so far came with no success.

Is anyone able to spit out an example code / clear instructions?
I just need a small push towards my goal!

Thanks in advance and kind regards,
Fer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I don't know if this can help you

        public async Task<ListaOfferteRootObject> GetListaOfferte (
            string chDitta,
            string ip,
            string chUtente,
            string password,
            string chSoggetto,
            string filtro,
            string ordinamento,
            string stato
        )
        {
            using (var client = new System.Net.Http.HttpClient (new NativeMessageHandler())) {
    
                try {
    
                    ListaOfferteRichiesta listaOfferteRichiesta = new ListaOfferteRichiesta();
                    listaOfferteRichiesta.ChDitta = chDitta;
                    listaOfferteRichiesta.ChSoggetto = chSoggetto;
                    listaOfferteRichiesta.ChUtente = chUtente;
                    listaOfferteRichiesta.filtro = filtro;
                    listaOfferteRichiesta.ordinamento = ordinamento;
                    listaOfferteRichiesta.PassWord = password;
                    listaOfferteRichiesta.stato = stato;
    
                    string json = JsonConvert.SerializeObject (listaOfferteRichiesta);
    
                    client.Timeout = TimeSpan.FromMilliseconds (TIMEOUT);
    
                    var content = new StringContent(json, Encoding.UTF8, "application/json");
    
                    var response = await client.PostAsync (ListaOfferteRootObject.HttpAddress (ip), content);
                    if(response.IsSuccessStatusCode) {
                        var responseString = response.Content.ReadAsStringAsync ().Result;
                        ListaOfferteRootObject offerte = new ListaOfferteRootObject ();
                        if (responseString != "") {
                            offerte = JsonConvert.DeserializeObject<ListaOfferteRootObject> (responseString);
                        }
                        return offerte;
                    }
                    else
                        throw new Exception(response.StatusCode.ToString());
                } catch (Exception ex) {
                    throw new Exception ("Riprova più tardi\r\nErrore nell'accesso al server: " + ex.Message);
                }
    
            }
        }
    
  • BenPiBenPi USUniversity ✭✭

    Can you show your webrequest code?
    That would be the easiest way to identify what goes wrong :)

Sign In or Register to comment.