Пример #1
0
        /// <summary>
        /// Gets the JSON list
        /// </summary>
        /// <returns>A <see cref="Task"/></returns>
        public async Task GetJson()
        {
            this.ButtonEnabled = false;
            string url = "http://www.google.co.uk";

            if (await NetworkStatus.HasConnectivity(url))
            {
                try
                {
                    Tuple <bool, string> getJsonResponse = await this.getJsonService.GetJsonAsync();

                    bool isSuccessful = getJsonResponse.Item1;
                    ObservableCollection <Player> players = new ObservableCollection <Player>();
                    if (isSuccessful)
                    {
                        string  content       = getJsonResponse.Item2;
                        JObject parsedPayload = JsonConvert.DeserializeObject(content) as JObject;

                        JToken data = parsedPayload.SelectToken("$.players");

                        if (data.HasValues)
                        {
                            if (data.Type == JTokenType.Array)
                            {
                                var dataArray = data as JArray;
                                players = JsonConvert.DeserializeObject <ObservableCollection <Player> >(dataArray.ToString());
                            }
                            else if (data.Type == JTokenType.Object)
                            {
                                var player = JsonConvert.DeserializeObject <Player>(data.ToString());
                                players.Add(player);
                            }
                        }

                        if (players.Count > 0)
                        {
                            this.PlayersList = players;
                            await this.StartChoosing(null);

                            this.ButtonEnabled = true;
                        }
                    }
                }
                catch (Exception)
                {
                    this.ButtonEnabled = true;
                    await App.Current.MainPage.DisplayAlert("Something went wrong", "Please try again", "OK");
                }
            }
            else
            {
                this.ButtonEnabled = true;
                await App.Current.MainPage.DisplayAlert("No internet", "You're internet connection is rubbish. Please try again", "OK");
            }
        }
Пример #2
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            var explorer = FindViewById <WebView>(Resource.Id.webView1);

            explorer.Settings.JavaScriptEnabled = true;

            var button     = FindViewById <Button>(Resource.Id.MyButton);
            var txtWelcome = this.FindViewById <TextView>(Resource.Id.txtWelcome);

            if (NetworkStatus.HasConnectivity(this))
            {
                TrelloUserInfoProxy = new TrelloOAuth();

                TrelloUserInfoProxy.OAuthFailed += delegate
                {
                    _progress.Hide();
                    Toast.MakeText(this, "Unable to login. OAuthentication failed...", ToastLength.Short).Show();
                    TrelloUserInfoProxy.Authenticate(false);
                };

                TrelloUserInfoProxy.LoggedIn += delegate(InfoUser infoUser)
                {
                    var dashboard = new Intent(this, typeof(DashboardActivity));
                    button.Text    = "Logout";
                    button.Enabled = true;
                    _progress.Hide();
                    txtWelcome.Text = string.Format("Welcome, {0}", infoUser.FullName);
                    Thread.Sleep(1000);
                    StartActivity(dashboard);
                };

                TrelloUserInfoProxy.UrlReady += delegate {
                    _progress.Hide();
                    button.Enabled = true;
                };

                this._progress = new ProgressDialog(this)
                {
                    Indeterminate = true
                };
                this._progress.SetTitle("Login In Progress");
                this._progress.SetMessage("Please wait...");
                this._progress.Show();

                TrelloUserInfoProxy.Authenticate(true);
            }
            else
            {
                Toast.MakeText(this, "Unable to reach internet...", ToastLength.Long).Show();
                button.Enabled = false;
            }

            button.Click += delegate
            {
                if (button.Text == "Logout")
                {
                    button.Text = "Login";
                    TrelloUserInfoProxy.ForceLogout();
                    TrelloUserInfoProxy.Authenticate(false);
                }
                else
                {
                    explorer.LoadUrl(TrelloUserInfoProxy.Url.ToString());
                    button.Enabled = false;
                    RunOnUiThread(() => { explorer.Visibility = ViewStates.Visible; });
                }
            };

            explorer.SetWebViewClient(new TrelloAuthViewClient());
        }