请问我的问题需要帮助!
我有以下链接;存储特定号码(数据)我将在我的Windows Phone应用程序中使用它。 http://jaradat.eb2a.com/read.php
如何读取链接中存储的最新号码(此号码会改变);并在我的Windows手机应用程序中显示它。
我是否应该使用webclient访问url中的数据,如下所示?
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
}
我如何阅读链接中的最新值?应该把它分成代币吗?
答案 0 :(得分:2)
您在问题中指出的方法对于从链接检索数据是正确的。虽然还有其他方法可以做到这一点
如果您想进一步了解,请参考以下内容。
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
您的问题似乎是关于如何检索响应中的最后一个值。 试试这个......
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]);
}
}
<强>观察强>
答案 1 :(得分:0)
就这样做
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];
}
}
希望得到它的帮助。