private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            // Note that you can also use the NJsonHttpMessageConverter based on Json.NET library
            // that supports getting/setting values from JSON directly,
            // without the need to deserialize/serialize to a .NET class.

            IHttpMessageConverter jsonConverter = new DataContractJsonHttpMessageConverter();
            jsonConverter.SupportedMediaTypes.Add(new MediaType("text", "javascript"));

            RestTemplate template = new RestTemplate();
            template.MessageConverters.Add(jsonConverter);

            #if NET_3_5
            template.GetForObjectAsync<GImagesResponse>("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q={query}",
                r =>
                {
                    if (r.Error == null)
                    {
                        this.ResultsItemsControl.ItemsSource = r.Response.Data.Items;
                    }
                }, this.SearchTextBox.Text);
            #else
            // Using Task Parallel Library (TPL)
            var uiScheduler = System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext();
            template.GetForObjectAsync<GImagesResponse>("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q={query}", this.SearchTextBox.Text)
                .ContinueWith(task =>
                {
                    if (!task.IsFaulted)
                    {
                        this.ResultsItemsControl.ItemsSource = task.Result.Data.Items;
                    }
                }, uiScheduler); // execute on UI thread
            #endif
        }
        public RestClient(string url = null)
        {
            string BaseUrl = string.Empty;
            var httpUrl = Properties.Settings.Default.HttpUrl;
            if (!string.IsNullOrEmpty(httpUrl))
            {
                BaseUrl = httpUrl;
            }
            else
            {
                BaseUrl = RestClient.httpUrl;
                Properties.Settings.Default.HttpUrl = RestClient.httpUrl;
                Properties.Settings.Default.Save();
            }

            BaseUrl = url ?? BaseUrl;

            restTemplate = new RestTemplate(BaseUrl);
            IHttpMessageConverter jsonConverter = new DataContractJsonHttpMessageConverter();
            jsonConverter.SupportedMediaTypes.Add(MediaType.APPLICATION_JSON);
            restTemplate.MessageConverters.Add(jsonConverter);
        }
        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            // Note that you can also use the NJsonHttpMessageConverter based on Json.NET library
            // that supports getting/setting values from JSON directly,
            // without the need to deserialize/serialize to a .NET class.

            IHttpMessageConverter jsonConverter = new DataContractJsonHttpMessageConverter();

            jsonConverter.SupportedMediaTypes.Add(new MediaType("text", "javascript"));

            RestTemplate template = new RestTemplate();

            template.MessageConverters.Add(jsonConverter);

#if NET_3_5
            template.GetForObjectAsync <GImagesResponse>("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q={query}",
                                                         r =>
            {
                if (r.Error == null)
                {
                    this.ResultsItemsControl.ItemsSource = r.Response.Data.Items;
                }
            }, this.SearchTextBox.Text);
#else
            // Using Task Parallel Library (TPL)
            var uiScheduler = System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext();
            template.GetForObjectAsync <GImagesResponse>("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q={query}", this.SearchTextBox.Text)
            .ContinueWith(task =>
            {
                if (!task.IsFaulted)
                {
                    this.ResultsItemsControl.ItemsSource = task.Result.Data.Items;
                }
            }, uiScheduler);     // execute on UI thread
#endif
        }
Example #4
0
        public RestClient(string url = null)
        {
            string BaseUrl = string.Empty;
            var    httpUrl = Properties.Settings.Default.HttpUrl;

            if (!string.IsNullOrEmpty(httpUrl))
            {
                BaseUrl = httpUrl;
            }
            else
            {
                BaseUrl = RestClient.httpUrl;
                Properties.Settings.Default.HttpUrl = RestClient.httpUrl;
                Properties.Settings.Default.Save();
            }

            BaseUrl = url ?? BaseUrl;

            restTemplate = new RestTemplate(BaseUrl);
            IHttpMessageConverter jsonConverter = new DataContractJsonHttpMessageConverter();

            jsonConverter.SupportedMediaTypes.Add(MediaType.APPLICATION_JSON);
            restTemplate.MessageConverters.Add(jsonConverter);
        }
	    public void SetUp() 
        {
            converter = new DataContractJsonHttpMessageConverter();
	    }