How to give await and async in the onclick button restsharp call has been made to the api ?

I dont know how to give await and async in my code so that loading screen appear, If I dont use await and async file uploads to the server but I want there loading screen to appear while uploading.

private ProgressDialog progressDialog;
protected override void OnCreate (Bundle savedInstanceState)
{

base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.ProfilePicSelectLayout);
this.progressDialog = new ProgressDialog (this);
this.progressDialog.SetMessage ("loading please wait...");

Button pictureok = FindViewById (Resource.Id.buttonimok);
pictureok.Click += async (sender, e) => {

                this.progressDialog.Show();


                Bitmap myBitmap;
                var bitmapDrawable = _imageView_Hall.Drawable as BitmapDrawable;
                myBitmap = bitmapDrawable.Bitmap;
                MemoryStream stream = new MemoryStream();
                myBitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
                byte[] bitmapData = stream.ToArray();


                RestRequest request = new RestRequest("api/profile/UpdateProfilePicture", Method.POST);
                request.AddParameter("StudentId", "20");
                request.AddFile("photo", bitmapData, "photo.jpg", "image/jpeg");
                RestClient restClient = new RestClient("http://XXX.com/");
                IRestResponse response = restClient.Execute(request);
                var content = response.Content;


                if (this.progressDialog.IsShowing) {
                    this.progressDialog.Hide();
                }
                StartActivity (typeof(SelectThemeActivity));
            }




        };

}

Best Answers

Answers

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    Well using async/await requires you to use actual async methods :smile:

    For example this line IRestResponse response = restClient.Execute(request); doesn't it have any Async method as well?

  • JSparrowJSparrow USMember

    then what can I use? instead of this. any suggestions

  • NMackayNMackay GBInsider, University mod
    edited April 2016

    @JSparrow

    Just use HTTPClient, all my rest calls are async.

     public async Task<DashboardSummary> GetDashboardSummary(CancellationToken ct)
            {
                using (var client = new HttpClient())
                {
                    var url = string.Format(_endpoint + "GetDashboardSummary");
    
                    HttpContent content = new StringContent("", Encoding.UTF8, WebConstants.ContentTypeJson);
    
                    var resp = await client.PostAsync(url, content, ct);
    
                    if (resp.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        throw new SecurityException(WebConstants.ServiceSecWarning, new Exception(resp.Content.ToString()));
                    }
    
                    var result =
                        JsonConvert.DeserializeObject<DashboardSummary>(resp.Content.ReadAsStringAsync().Result);
                    return result;
                }
            }
    
Sign In or Register to comment.