Created
May 12, 2018 18:43
-
-
Save shanmugasanthosh7/34d134760914db92037a99d3b9c25df5 to your computer and use it in GitHub Desktop.
Call API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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