コード例 #1
0
        /*When the main page loads, it calls the GetJsonData() method which is defined.
         * This method uses the HttpClient.getAsync method to the web API specified in the
         * URL link to send a GET request. In response, the web API sends a HttpResponseMessage
         * object. This response contains content which is then de-serialized into a list to get the actual
         * question and options set which is used to load into the Page1.xaml
         */

        public async void GetJsonData()
        {
            // If device is connected to the Internet
            if (NetworkCheck.IsInternet())
            {
                try
                {
                    HttpClient          client   = new System.Net.Http.HttpClient();
                    HttpResponseMessage response = await client.GetAsync(jsonURI);

                    // If HTTP response is succesful, seraialize the HTTP content to the the string - JsonString
                    if (response.IsSuccessStatusCode)
                    {
                        string JsonString = await response.Content.ReadAsStringAsync();

                        //Converting JSON array objects into generic list
                        quizQuestionList = JsonConvert.DeserializeObject <QuizQuestionList>(JsonString);
                    }
                }

                catch (Exception ex)
                {
                    Debug.WriteLine("\tERROR {0}", ex.Message);
                }
            }
            else
            {
                //Device is not connected to the internet.
                await DisplayAlert("JSONParsing", "No Network is available.", "Ok");
            }

            QuizPageViewModel.PopulateQuestionSet(quizQuestionList);
        }
コード例 #2
0
ファイル: Page1.xaml.cs プロジェクト: brekooname/QuizzingFun
 public Page1()
 {
     InitializeComponent();
     quizPageViewModel = new QuizPageViewModel();
     BindingContext    = quizPageViewModel;
 }