0
votes

Please I need help in my issue!

I have the following link ; which stored specific numbers (data) I will use it in my windows phone application. http://jaradat.eb2a.com/read.php

How I can read the latest number stored in the link (this number will change) ; and show it in my windows phone application.

Should I use the webclient to access the data in url like the following ?

 WebClient wc = new WebClient();
            wc.DownloadStringCompleted += HttpCompleted;
            wc.DownloadStringAsync(new Uri("http://jaradat.eb2a.com/read.php"));

 private void HttpCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {


            // do something  here
        }

And How I can read the latest value in the link ? should divide it to tokens ?

2

2 Answers

0
votes

Just do it

        WebClientwc = new WebClient();
        wc.DownloadStringCompleted += HttpCompleted;
        wc.DownloadStringAsync(new Uri("http://jaradat.eb2a.com/read.php"));

    private void HttpCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            var resultString = e.Result;
            var parts = resultString.Split('"').Select(n => n).ToArray();
            int[] resultIntArrayFirst = parts[1].Split(',').Select(n => Convert.ToInt32(n)).ToArray();
            double [] resultIntArraySecond = parts[3].Split(',').Select(Convert.ToDouble).ToArray();
             double lastValue = resultIntArraySecond[resultIntArraySecond.Length - 1];
        }
    }

Hope its help.

2
votes

The method you have indicated in your question is correct for retrieving the data from the link. Though there are other ways of doing it.
Here are some references if you want to learn further.
Making a HTTP request and listening its completion in Windows Phone
HttpWebRequest Fundamentals - Windows Phone Services Consumption - Part 1
HttpWebRequest Fundamentals - Windows Phone Services Consumption - Part 2

Your question seems to be about how to retrieve the last value in the response. Try this...

private void HttpCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error == null)
    {
       //this will break the string into two tokens
        string[] first_lot = e.Result.Split('"');

        //assuming you want to read the first lot first_lot[0].Split(','); 
        // seccond lot  first_lot[1].Split(',');
        string[] numbers = first_lot[0].Split(',');

        int last_digit = int.Parse(numbers[numbers.Length - 1]);

    }
}

Observations

  1. If possible, tweak the server code to return only one digit. It will save the application user a lot of data costs.
  2. Consider using JSON data format as the response format on the server side code.