当前位置: 首页>>代码示例>>C#>>正文


C# RestTemplate.GetForObjectAsync方法代码示例

本文整理汇总了C#中RestTemplate.GetForObjectAsync方法的典型用法代码示例。如果您正苦于以下问题:C# RestTemplate.GetForObjectAsync方法的具体用法?C# RestTemplate.GetForObjectAsync怎么用?C# RestTemplate.GetForObjectAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在RestTemplate的用法示例。


在下文中一共展示了RestTemplate.GetForObjectAsync方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SearchButton_Click

        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
        }
开发者ID:gabrielgreen,项目名称:spring-net-rest,代码行数:34,代码来源:MainWindow.xaml.cs

示例2: SearchButton_Click

        private void SearchButton_Click(object sender, EventArgs e)
        {
            this.ResultsFlowLayoutPanel.Controls.Clear();

            IHttpMessageConverter njsonConverter = new NJsonHttpMessageConverter();
            njsonConverter.SupportedMediaTypes.Add(new MediaType("text", "javascript"));

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

            template.GetForObjectAsync<JToken>("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q={query}",
                delegate(RestOperationCompletedEventArgs<JToken> r)
                {
                    if (r.Error == null)
                    {
                        foreach (JToken jToken in r.Response.Value<JToken>("responseData").Value<JArray>("results"))
                        {
                            PictureBox pBox = new PictureBox();
                            pBox.ImageLocation = jToken.Value<string>("tbUrl");
                            pBox.Height = jToken.Value<int>("tbHeight");
                            pBox.Width = jToken.Value<int>("tbWidth");

                            ToolTip tt = new ToolTip();
                            tt.SetToolTip(pBox, jToken.Value<string>("visibleUrl"));

                            this.ResultsFlowLayoutPanel.Controls.Add(pBox);
                        }
                    }
                }, this.SearchTextBox.Text);

            /*
            template.GetForObjectAsync<GImagesResponse>("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q={query}",
                delegate(RestOperationCompletedEventArgs<GImagesResponse> r)
                {
                    if (r.Error == null)
                    {
                        foreach (GImage gImage in r.Response.Data.Items)
                        {
                            PictureBox pBox = new PictureBox();
                            pBox.ImageLocation = gImage.ThumbnailUrl;
                            pBox.Height = gImage.ThumbnailHeight;
                            pBox.Width = gImage.ThumbnailWidth;

                            ToolTip tt = new ToolTip();
                            tt.SetToolTip(pBox, gImage.SiteUrl);

                            this.ResultsFlowLayoutPanel.Controls.Add(pBox);
                        }
                    }
                }, this.SearchTextBox.Text);
            */
        }
开发者ID:nsavga,项目名称:spring-net-rest,代码行数:52,代码来源:GoogleImages.cs

示例3: Main

        static void Main(string[] args)
        {
            try
            {
                // Configure RestTemplate by adding the new converter to the default list
                RestTemplate template = new RestTemplate();
                template.MessageConverters.Add(new ImageHttpMessageConverter());

                // Get image from url
#if NET_4_0
                Bitmap nuGetLogo = template.GetForObjectAsync<Bitmap>("http://nuget.org/Content/Images/nugetlogo.png").Result;
#else
                Bitmap nuGetLogo = template.GetForObject<Bitmap>("http://nuget.org/Content/Images/nugetlogo.png");
#endif

                // Save image to disk
                string filename = Path.Combine(Environment.CurrentDirectory, "NuGetLogo.png");
                nuGetLogo.Save(filename);
                Console.WriteLine(String.Format("Saved NuGet logo to '{0}'", filename));
            }
#if NET_4_0
            catch (AggregateException ae)
            {
                ae.Handle(ex =>
                {
                    if (ex is HttpResponseException)
                    {
                        Console.WriteLine(((HttpResponseException)ex).GetResponseBodyAsString());
                        return true;
                    }
                    return false;
                });
            }
#else
            catch (HttpResponseException ex)
            {
                Console.WriteLine(ex.GetResponseBodyAsString());
            }
#endif
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
			finally
			{
				Console.WriteLine("--- hit <return> to quit ---");
				Console.ReadLine();
			}
        }
开发者ID:nsavga,项目名称:spring-net-rest,代码行数:49,代码来源:Program.cs

示例4: OnActivate

        protected override void OnActivate()
        {
            base.OnActivate();

            RestTemplate template = new RestTemplate(Api.Host);
            template.MessageConverters.Add(new NJsonHttpMessageConverter());

            var body = new GetRiddlesRequest();
            HttpEntity entity = new HttpEntity(body);
            entity.Headers.Add("X-Auth-Token", _registrationService.GetToken());

            template.GetForObjectAsync<GetRiddlesResponse>("/riddles", r =>
            {
                if(r.Error == null)
                {
                    Execute.OnUIThread( () =>
                    {
                        RiddleListView view = (RiddleListView)GetView();
                        ListBox riddlesList = view.riddles;
                        foreach (var riddle in r.Response.riddles)
                        {
                            StackPanel panel = new StackPanel();
                            panel.Children.Add(new TextBlock() {Text = riddle.author});

                            Image img = new Image();
                            panel.Children.Add(img);
                            panel.Tap += OnRiddleTap;
                            panel.Tag = riddle;

                            riddlesList.Items.Add(panel);

                            DownloadImage(img, riddle.photo_url);

                        }
                    } );
                }
            });
        }
开发者ID:rawman,项目名称:HackkrkGuessWP7,代码行数:38,代码来源:RiddleListViewModel.cs


注:本文中的RestTemplate.GetForObjectAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。