Need help.. This is my first application ever. I'm trying to populate GridView with JSON data. code below works, but now I'm trying to move the async private void haePostimerkitPilvesta()
and public static string ReadStreamAsString(Stream input)
method code blocks from MainPage.xaml.cs
to other .cs file with no luck.. How should I write the code so that I can call it correctly? without async method I am able to do the same, but then the code does not work.
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
haePostimerkitPilvesta();
}
async private void haePostimerkitPilvesta()
{
Uri address = new Uri("xxx.json"); //public link of our file
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
WebResponse response = await request.GetResponseAsync();
Stream stream = response.GetResponseStream();
string content = ReadStreamAsString(stream);
GridViewPostimerkit.ItemsSource = JsonConvert.DeserializeObject(content, typeof(List<Postimerkit>));
}
public static string ReadStreamAsString(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return Encoding.UTF8.GetString(ms.ToArray(), 0, ms.ToArray().Count());
}
}