locked
How to Make HttpClient make Get Request Daily

    Question

  • Hi,

    I need to get live data from Asp.net server. I am using HttpClient.  How to make HttpClient Request every half hour background after App is launched or Close. The data retrieved will feed to Live Tile .

    1) Using HttpClient to make a Get Request with below URL:

    Example : URL is : http://www.School.com/srestservice/sservice/edt1/getlessonplanupdateswin8/20000101010000";


    2) Using AspNet to return the below XML 

    <?xml version="1.0" encoding="utf-8"?>
    <tile>
       <visual version="2">
         <binding template="TileSquare150x150Block" fallback="TileSquareBlock">
           <text id="1">Text Field 1 (block text)</text>
           <text id="2">Text Field 2</text>
         </binding> 
       </visual>
     </tile>


    3) How to I use the Return Data for Live Tile which base on this Template :TileSquare150x150Block

        How to request every half hour in background ? I want the live Tile to display the data return from the Server.

       string StrURL = "http://www.School.com/srestservice/sservice/edt1/getlessonplanupdateswin8/20000101010000"

       Uri polledUri = new Uri(StrURL);

       var httpClient = new System.Net.Http.HttpClient();

       var response = await httpClient.GetAsync(polledUri);
     
       response.EnsureSuccessStatusCode();

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

    if HttpClient is not a good choice, How to I use Periodic Live Tile?

      What XML data Format must AspNet to return ? a or b ?

    a)

    <?xml version="1.0" encoding="utf-8"?>
    <tile>
       <visual version="2">
         <binding template="TileSquare150x150Block" fallback="TileSquareBlock">
           <text id="1">Text Field 1 (block text)</text>
           <text id="2">Text Field 2</text>
         </binding> 
       </visual>
     </tile>

    b)

    <tile>
       <visual version="2">
         <binding template="TileSquare150x150Block" fallback="TileSquareBlock">
           <text id="1">Text Field 1 (block text)</text>
           <text id="2">Text Field 2</text>
         </binding> 
       </visual>
     </tile>

    Could not find any sample code on Live Tile working with Asp.net or PHP.

    Would appreciate your help.

    Thanks

    Thanks

    Monday, March 23, 2015 3:12 PM

All replies

  • Hi FireDance,

    I think you may need this: How to run a background task on a timer

    You can schedule a HttpClient request runs up to every 15 minutes on Windows and every 30 minutes on Windows Phone, in your scenario, every half hour is 30 mins.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, March 24, 2015 8:30 AM
    Moderator
  • Hi Jamles,

    I have followed all the steps.

    I Have added a new project Windows Runtime Component in my Solution.
    I have added the reference to this project

    I have declared the Background Task in Manifest

    Nothing happen! What seems to be the problem??


    ---- In MainPage.xaml.cs

    protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                base.OnNavigatedTo(e);

                CheckTaskRegistration();
            }

            private void CheckTaskRegistration()
            {
                foreach (var task in BackgroundTaskRegistration.AllTasks)
                {
                    if (task.Value.Name == "TileUpdater")
                    {
                        isTaskRegistered = true;
                        break;
                    }
                }


                if (isTaskRegistered == false)
                {
                    RegisterBackgroundTask("TileUpdater", "BackgroundTasks.TileUpdater");
                }
            }


            private void RegisterBackgroundTask(string name, string entrypoint)
            {
                BackgroundTaskBuilder btb = new BackgroundTaskBuilder();

                btb.Name = name;
                btb.TaskEntryPoint = entrypoint;
                btb.SetTrigger(new SystemTrigger(SystemTriggerType.InternetAvailable, false));           
                BackgroundTaskRegistration task = btb.Register();
                task.Progress += task_Progress;
                task.Completed += task_Completed;
            }

            void task_Completed(BackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs args)
            {
                //IMPLEMENT SOME CODE TO RUN AFTER YOUR BACKGROUND AGENT COMPLETES.
            }


            void task_Progress(BackgroundTaskRegistration sender, BackgroundTaskProgressEventArgs args)
            {

            }


    ----------- In Background Task project


    namespace BackgroundTasks
    {
        class TileUpdater : IBackgroundTask
        {
            string strLesson;

            public void Run(IBackgroundTaskInstance taskInstance)
            {
                BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
                //
                // TODO: Insert code to start one or more asynchronous methods using the
                //       await keyword, for example:
                //
                // await ExampleMethodAsync();
                //        

                retrieveUpdate();

                //First, we grab the specific template we want to use.
                XmlDocument tileData = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquare150x150Block);
                tileData.ToString();

                //Then we grab a reference to the node we want to update.
                XmlNodeList textData = tileData.GetElementsByTagName("text");


                //Then we set the value of that node.
                textData[0].InnerText = "1";
                textData[1].InnerText = "Updated Lessons";

                //Then we create a TileNotification object with that data.
                TileNotification notification = new TileNotification(tileData);

                //We can optionally set an expiration date on the notification.
                //notification.ExpirationTime = DateTimeOffset.UtcNow.AddSeconds(30);

                TileUpdateManager.CreateTileUpdaterForApplication().EnableNotificationQueue(true);

                //Finally, we push the update to the tile.
                TileUpdateManager.CreateTileUpdaterForApplication().Update(notification);

                _deferral.Complete();
           
            }

            private async void retrieveUpdate()
            {       

                Uri polledUri = new Uri("http://www.School.com/ssrestservice/sservice/edt1/getlessonplanupdateswin8/20000101010000");

                var httpClient = new System.Net.Http.HttpClient();
                var response = await httpClient.GetAsync(polledUri);

                //will throw an exception if not successful
                response.EnsureSuccessStatusCode();

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

                string[] strArray1 = content.Split('>');

                string[] strTarget = strArray1[1].Split('<');

                string strResult = strTarget[0].ToString();

                strLesson = strResult;

            }

    PLease help

    Wednesday, March 25, 2015 3:01 PM
  • Hi FireDance,

    Sorry for the late response, do you have a mini-repro that I can give a test? Hardly to find any mistake simply read the code on the forum.

    By the way you can always do some self check How to debug a background task

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, March 31, 2015 4:07 PM
    Moderator