C#でUWPのWebAPIを調べるため、ネットにあったサンプルを利用しようとしているのですが、「'await' 演算子は、非同期メソッド内でのみ使用できます。このメソッドに 'async' 修飾子を指定し、戻り値の型を 'Task' に変更することを検討してください。」とエラーが出ます。
まだ勉強中なため、根本がわかっていないのだと思いますが、このエラーは何が悪いためのエラーなのでしょうか?
初心者にわかりやすく教えて頂けるとありがたいです。
よろしくお願いいたします。
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; using Windows.Web.Http; using Newtonsoft.Json.Linq; // 空白ページのアイテム テンプレートについては、http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 を参照してください namespace MaterialMatching_WebAPI { /// <summary> /// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。 /// </summary> public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } private void button_Click(object sender, RoutedEventArgs e) { //ここにボタンがクリックされた時の処理を書く string apiKey = "key"; string zipCode = "123-4567,jp"; string endpoint = string.Format( "http://api.openweathermap.org/data/2.5/weather?zip={0}&appid={1}", zipCode, apiKey ); //HttpClientを使って天気情報を取得する HttpClient client = new HttpClient(); HttpResponseMessage result = await client.GetAsync(new Uri(endpoint)); string json = await result.Content.ReadAsStringAsync(); //jsonを解析して結果を取得する JObject root = JObject.Parse(json); string weather = root["weather"].First()["main"].ToString(); textWeather.Text = weather; } } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/13 12:37
2017/12/13 13:13 編集
2017/12/13 13:12
2017/12/14 11:26
2017/12/14 18:57 編集