android - Using async await C# Xamairn -



android - Using async await C# Xamairn -

i have started using xamarin , quite new c#. want create asynchronous http request, started using async , await httpwebrequest. don't asynchronous called, ui getting block. code i'm testing. have 2 implementation requestcitas() should asynchronous , srequestcitas() synchronous, wrong?. thanks.

namespace mc { [activity (label = "mc", mainlauncher = true, theme="@android:style/theme.holo.light")] public class mainactivity : activity { list<cita> citas; citaadapter adapter; protected override void oncreate (bundle savedinstancestate) { base.oncreate (savedinstancestate); setcontentview (resource.layout.main); citas = new list<cita> (); adapter = new citaadapter (this, citas); listview listview = findviewbyid<listview> (resource.id.main_citas); listview.adapter = adapter; var c1 = new cita (0, "ernesto", "de los santos", "19/2/14", "19/2/14", "09:28", "10:45"); var c2 = new cita (1, "ernesto", "de los santos", "19/2/14", "19/2/14", "09:28", "10:45"); var c3 = new cita (2, "ernesto", "de los santos", "19/2/14", "19/2/14", "09:28", "10:45"); var c4 = new cita (3, "ernesto", "de los santos", "19/2/14", "19/2/14", "09:28", "10:45"); var c5 = new cita (4, "ernesto", "de los santos", "19/2/14", "19/2/14", "09:28", "10:45"); citas.add (c1); citas.add (c2); citas.add (c3); citas.add (c4); citas.add (c5); console.writeline ("notificando al adaptador"); adapter.notifydatasetchanged (); string result; findviewbyid<button> (resource.id.button1).click += async (sender, e) => { result = await requestcitas (); }; findviewbyid<button> (resource.id.button2).click += delegate { result = srequestcitas (); }; } private async task<string> requestcitas () { // var url = "http://192.168.1.126:8081"; var url = "http://192.168.1.124"; //var method = "/citas/get"; var method = ""; var encoding = new utf8encoding (); byte[] parametros = encoding.getbytes ("usersid=97"); var request = (httpwebrequest)webrequest.create (url + method); request.method = "post"; request.contenttype = "application/x-www-form-urlencoded"; request.contentlength = parametros.length; var datastream = request.getrequeststream (); datastream.write (parametros, 0, parametros.length); datastream.close (); string str = ""; task<webresponse> task = request.getresponseasync (); webresponse newresponse = await task; var responsestream = newresponse.getresponsestream (); var streamreader = new streamreader (responsestream, system.text.encoding.getencoding ("utf-8")); char[] read = new char[256]; int count = streamreader.read (read, 0, 256); while (count > 0) { str += new string (read, 0, count); count = streamreader.read (read, 0, 256); } responsestream.close (); homecoming str; } private string srequestcitas () { var url = "http://192.168.1.126:8081"; var method = "/citas/get"; var encoding = new utf8encoding (); byte[] parametros = encoding.getbytes ("usersid=97"); var request = (httpwebrequest)webrequest.create (url + method); request.method = "post"; request.contenttype = "application/x-www-form-urlencoded"; request.contentlength = parametros.length; var datastream = request.getrequeststream (); datastream.write (parametros, 0, parametros.length); datastream.close (); string str = ""; httpwebresponse newresponse = (httpwebresponse)request.getresponse (); var responsestream = newresponse.getresponsestream (); var streamreader = new streamreader (responsestream, system.text.encoding.getencoding ("utf-8")); char[] read = new char[256]; int count = streamreader.read (read, 0, 256); while (count > 0) { str += new string (read, 0, count); count = streamreader.read (read, 0, 256); } responsestream.close (); homecoming str; } } }

you're getting response using async method, you're reading stream using synchronous stream operations. seek using streamreader.readasync instead.

or, if you're feeling ambitious, seek replacing whole while loop altogether , use:

var str = await streamreader.readtoendasync();

on side note, streamreader implements idisposable , you're not disposing of instance when you're done it. leaking native resources.

c# android xamarin async-await

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -