Monday, January 05, 2009

Connecting and reading run data from NikePlus in C#

There's already quite a few posts and examples out there in the blogosphere for how to read data from NikePlus, so I won't explain this in great detail.

But in case anyone is interested, then here's my first stab at some code to do it:

            CookieContainer cookies = new CookieContainer();

 

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(

                string.Format("https://secure-nikeplus.nike.com/services/profileService?action=login&login={0}&password={1}&locale=en_us",

                "your user name here",

                "your password here"));

            request.CookieContainer = cookies;

 

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())

            {

                Stream s = response.GetResponseStream();

                StreamReader reader = new StreamReader(s);

                string text = reader.ReadToEnd();

            }

 

 

            HttpWebRequest requestRunList = (HttpWebRequest)WebRequest.Create(

                "https://secure-nikeplus.nike.com/nikeplus/v1/services/app/run_list.jhtml");

            requestRunList.CookieContainer = cookies;

 

            using (HttpWebResponse responseRunList = (HttpWebResponse)requestRunList.GetResponse())

            {

                Stream s = responseRunList.GetResponseStream();

                StreamReader reader = new StreamReader(s);

                string text = reader.ReadToEnd();

                Debug.WriteLine(text);

            }

 

            HttpWebRequest requestRun = (HttpWebRequest)WebRequest.Create(string.Format(

                "https://secure-nikeplus.nike.com/nikeplus/v1/services/app/get_run.jhtml?id={0}",

                "pick a run from the run list retrieved above"

                ));

            requestRun.CookieContainer = cookies;

 

            using (HttpWebResponse responseRun = (HttpWebResponse)requestRun.GetResponse())

            {

                Stream s = responseRun.GetResponseStream();

                StreamReader reader = new StreamReader(s);

 

                XmlSerializer xmlS = new XmlSerializer(typeof(plusService));               

                plusService plusService = (plusService)xmlS.Deserialize(reader);

 

                Debug.WriteLine("status = " + plusService.status);

                Debug.WriteLine("datatype = " + plusService.sportsData.extendedDataList.extendedData.dataType);

                Debug.WriteLine("intervaltype = " + plusService.sportsData.extendedDataList.extendedData.intervalType);

                Debug.WriteLine("intervalUnit = " + plusService.sportsData.extendedDataList.extendedData.intervalUnit);

                Debug.WriteLine("intervalValue = " + plusService.sportsData.extendedDataList.extendedData.intervalValue);

                string[] items = plusService.sportsData.extendedDataList.extendedData.Value.Split(',');

                foreach (string item in items)

                    Debug.WriteLine("\t" + item);              

            }

        }

 

Where plusService is a class generated using "xsd.exe /classes" on an .xsd file generated from some example xml.

Note: maybe I should explain and expand upon this a bit better - I will when I tidy up the code a bit!

2 comments:

  1. this is one of the most informative and useful site
    IT trends

    ReplyDelete
  2. The blog is marvelous and enthusiastic.First time i visit your blog and love it.I ll also tell my

    friends to visit your blog and read it........................

    Software Solution

    ReplyDelete