Skip to content

Instantly share code, notes, and snippets.

@shanmugasanthosh7
Created May 12, 2018 18:43
Show Gist options
  • Save shanmugasanthosh7/34d134760914db92037a99d3b9c25df5 to your computer and use it in GitHub Desktop.
Save shanmugasanthosh7/34d134760914db92037a99d3b9c25df5 to your computer and use it in GitHub Desktop.
Call API
public class MainActivity : AppCompatActivity
{
protected override async void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main)
var textview = FindViewById<TextView>(Resource.Id.textView);
var apiService = NetworkService.GetApiService();
await apiService.GetPost()
.ContinueWith(post =>
{
if (post.IsCompleted && post.Status == TaskStatus.RanToCompletion)
{
// Get result and update any UI here.
var post = post.Result;
textView.Text = post.Title; // For property serialized/deserialized using Newtonsoft.Json
}
else if (post.IsFaulted)
{
// If any error occurred exception throws.
}
else if (post.IsCanceled)
{
// Task cancelled
}
},TaskScheduler.FromCurrentSynchronizationContext())// execute in main/UI thread.
.ConfigureAwait(false)// Execute API call on background or worker thread.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment