本文整理汇总了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
}
示例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);
*/
}
示例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();
}
}
示例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);
}
} );
}
});
}