GetData from server Windows 8

0

I have next function:

private void getAllData()
    {
        HttpWebRequest request = HttpWebRequest.CreateHttp("http://webservice.com/wfwe");
        request.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), request);
    }
        void GetResponsetStreamCallback(IAsyncResult callbackResult)
    {
        HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
        using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
        {
            string result = httpWebStreamReader.ReadToEnd();
            GetApplications(result);
        }
    }

And i fill in stack panels:

private void GetApplications(string result)
    {
        var ApplicationsList = JsonConvert.DeserializeObject<List<Applications>>(result);
        foreach (Applications A in ApplicationsList)
        {
            foreach (ApplicationRelation SCA in A.ApplicationRelations)
            {
                if (SCA.ApplicationSubcategory != null)
                {
                    #region Fill Customer Research Stack
                    if (SCA.ApplicationSubcategory.subcategoryName == "Customer Research")
                    {
                        if (TestStack.Children.Count == 0)
                        {
                            ApplicationTile AT = FillDataForApplicationTile(SCA);                                
                            AT.Margin = new Thickness(5, 0, 5, 0);
                            TestStack.Children.Add(AT);
                        }
                    }
                    #endregion
                }
            }
        }
    }

And code fails at:

if (TestStack.Children.Count == 0)

Error: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

How can i rewrite my Request from void to string, so i could do something like this:

GetApplications(await getAllData())

EDIT 2 for dcastro:

enter image description here

EDIT 3:

Thanks it works, but i was looking for something like this:

//Modified your code:

GetApplications(getAllData2().Result);

private async Task<string> getAllData2()
    {
       string uri = "http://webservice.com/wfe";
       var client = new HttpClient();
       HttpResponseMessage response = await client.GetAsync(uri);
       var result = await response.Content.ReadAsStringAsync();
       return result.ToString();
    }

But somehow my construction doesn't enter GetApplication function...

c#
windows-8
windows-8.1
asked on Stack Overflow Sep 17, 2013 by Cheese • edited Dec 9, 2013 by BenMorel

2 Answers

1

Instead of using AsyncCallback (which I'm pretty sure is running GetResponsetStreamCallback in a non-UI thread), try fetching your data like this:

private async void getAllData()
   string uri = "http://webservice.com/wfwe";
   var client = new HttpClient();

   HttpResponseMessage response = await client.GetAsync(uri);

   string body = await response.Content.ReadAsStringAsync();

   GetApplications(body);
}

This will call your webservice asynchronously (at line await Client.sendMessageAsync(msg);), and return to the original UI thread when the response is received. This way, you can update UI elements, like your TestStack.

Edit fixed bug

answered on Stack Overflow Sep 17, 2013 by dcastro • edited Sep 17, 2013 by dcastro
0

Try this.

private async Task<string> getAllData()
{
    string Result = "";
    var http = new HttpClient();
    var response = await http.GetAsync("http://webservice.com/wfwe"); // I am considering this URL gives me JSON
    if (response.StatusCode == System.Net.HttpStatusCode.OK)
    {
        Result = await response.Content.ReadAsStringAsync(); // You will get JSON here
    }
    else
    {
        Result = response.StatusCode.ToString(); // Error while accesing the web service.
    }

    return Result;
}

private async Task GetApplications(string result)
{
    var ApplicationsList = JsonConvert.DeserializeObject<List<Applications>>(result);
    foreach (Applications A in ApplicationsList)
    {
        foreach (ApplicationRelation SCA in A.ApplicationRelations)
        {
            if (SCA.ApplicationSubcategory != null)
            {
                #region Fill Customer Research Stack
                if (SCA.ApplicationSubcategory.subcategoryName == "Customer Research")
                {
                    if (TestStack.Children.Count == 0)
                    {
                        // This will update your UI using UI thread
                        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
                        {
                            ApplicationTile AT = FillDataForApplicationTile(SCA);
                            AT.Margin = new Thickness(5, 0, 5, 0);
                            TestStack.Children.Add(AT);
                        });
                    }
                }
                #endregion
            }
        }
    }
}
answered on Stack Overflow Sep 17, 2013 by Farhan Ghumra

User contributions licensed under CC BY-SA 3.0