Tuesday, July 15, 2014

Code Sample: Return Office element from Json query in Windows Phone

I wanted to capture this deprecated piece of code which I no longer need in my phone app.
We are supplying a list of office specific information to the phone client via a JSON query.

Calling Method:


using System.Net.WebClient;
using Newtonsoft.Json.JsonConvert;

private static Dictionary<int, Office> _officeLookup;
private static List<Office> theOffices = new List<Office>();

WebClient webClient = new WebClient();
webClient.Headers["Accept"] = "application/json";
webClient.DownloadStringCompleted +=
              new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri(apiUrl));



private void webClient_DownloadStringCompleted(object sender,             DownloadStringCompletedEventArgs e);
{
      try
      {
           if(e.Result != null)
          {
           var offices = JsonConvert.DeserializeObject<Office[]>(e.Result);
           theOffices = offices.ToList();
           int id = 0;
           foreach(Office office in offices)
           {
                _officeLookup.Add(id,office);
               id++;
             }
          }
         }
         catch (Exception ex)
         {
               //Error handling here
         }


}

No comments:

Post a Comment