C# 在C中每5秒进行一次API调用#
我正在尝试在Xamarin Forms平台上开发一个移动应用程序。我从API获取数据。API调用仅在应用程序打开时发生一次。我在ListView中列出了团队之间的livescores。比如现在比赛的时间是25分钟,我等了2分钟,什么都没变。分钟数在我的ListView上是相同的。当我再次关闭并打开应用程序时,分钟数正在变化。我只想每5秒钟打一次电话刷新数据,而不关闭应用程序。 这是我的密码C# 在C中每5秒进行一次API调用#,c#,api,xamarin,call,C#,Api,Xamarin,Call,我正在尝试在Xamarin Forms平台上开发一个移动应用程序。我从API获取数据。API调用仅在应用程序打开时发生一次。我在ListView中列出了团队之间的livescores。比如现在比赛的时间是25分钟,我等了2分钟,什么都没变。分钟数在我的ListView上是相同的。当我再次关闭并打开应用程序时,分钟数正在变化。我只想每5秒钟打一次电话刷新数据,而不关闭应用程序。 这是我的密码 public List<liveScoreData>liveScore() {
public List<liveScoreData>liveScore()
{
var result = new List<liveScoreData>();
try
{
Guid guidSifre = Guid.NewGuid();
string guid = guidSifre.ToString();
string result = CreateMD5forChecksum(guid);
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["result"] = result;
values["guid"] = guid;
var response = client.UploadValues("http://abcd.com/admin/LiveScore", values);
var responseString = Encoding.Default.GetString(response);
var responseResult = JsonConvert.DeserializeObject(responseString);
result = JsonConvert.DeserializeObject<List<liveScoreData>>(responseResult.ToString());
Mehmet.liveScoreDataList = result;
}
}
catch (Exception ex)
{
var exc = ex;
}
return result;
}
publicListLiveScore()
{
var result=新列表();
尝试
{
Guid guidSifre=Guid.NewGuid();
字符串guid=guidSifre.ToString();
字符串结果=CreateMD5forChecksum(guid);
使用(var client=new WebClient())
{
var values=新的NameValueCollection();
值[“结果”]=结果;
值[“guid”]=guid;
var response=client.UploadValues(“http://abcd.com/admin/LiveScore“、价值观);
var responseString=Encoding.Default.GetString(响应);
var responseResult=JsonConvert.DeserializeObject(responseString);
结果=JsonConvert.DeserializeObject(responseResult.ToString());
Mehmet.liveScoreDataList=结果;
}
}
捕获(例外情况除外)
{
var exc=ex;
}
返回结果;
}
您可以实现我的特殊PollingTimer.cs类:
using System;
using System.Threading;
using Xamarin.Forms;
namespace AppNamespace.Helpers
{
/// <summary>
/// This timer is used to poll the middleware for new information.
/// </summary>
public class PollingTimer
{
private readonly TimeSpan timespan;
private readonly Action callback;
private CancellationTokenSource cancellation;
/// <summary>
/// Initializes a new instance of the <see cref="T:CryptoTracker.Helpers.PollingTimer"/> class.
/// </summary>
/// <param name="timespan">The amount of time between each call</param>
/// <param name="callback">The callback procedure.</param>
public PollingTimer(TimeSpan timespan, Action callback)
{
this.timespan = timespan;
this.callback = callback;
this.cancellation = new CancellationTokenSource();
}
/// <summary>
/// Starts the timer.
/// </summary>
public void Start()
{
CancellationTokenSource cts = this.cancellation; // safe copy
Device.StartTimer(this.timespan,
() => {
if (cts.IsCancellationRequested) return false;
this.callback.Invoke();
return true; // or true for periodic behavior
});
}
/// <summary>
/// Stops the timer.
/// </summary>
public void Stop()
{
Interlocked.Exchange(ref this.cancellation, new CancellationTokenSource()).Cancel();
}
}
}
这将每5秒运行一次您的方法。要使方法与pollingtimer一起工作,必须将方法编辑为void,并将值返回到全局变量,如下所示:
//Make a global variable for your method to access
List<liveScoreData> globalLiveScore = new List<liveScoreData>();
public void liveScore()
{
var result = new List<liveScoreData>();
try
{
Guid guidSifre = Guid.NewGuid();
string guid = guidSifre.ToString();
string result = CreateMD5forChecksum(guid);
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["result"] = result;
values["guid"] = guid;
var response = client.UploadValues("http://abcd.com/admin/LiveScore", values);
var responseString = Encoding.Default.GetString(response);
var responseResult = JsonConvert.DeserializeObject(responseString);
result = JsonConvert.DeserializeObject<List<liveScoreData>>(responseResult.ToString());
Mehmet.liveScoreDataList = result;
}
}
catch (Exception ex)
{
var exc = ex;
}
globalLiveScore = result;
}
在你的OnDisappearing方法中,你可以运行
timer.Stop();
玩一玩,看看是否可以将其放置在更好的位置以获得最佳性能等。您可以使用
System.Threading.Timer
对象
然后像这样初始化它
`System.Threading.Timer myTimer = new System.Threading.Timer((e) =>
{
liveScore(); //your function call
}, null,
TimeSpan.FromSeconds(0), //start immediately
TimeSpan.FromSeconds(5)); //execute every 5 secs`
为什么要获取一个字符串,反序列化为对象,将其转换回字符串,然后再次反序列化?您在此处失去了我的投票权:
…您必须将方法编辑为空,并将值返回到全局变量…
。您可以通过使其通用化,表示其轮询对象的返回类型,并提供一个方法来调用每次轮询的结果,从而极大地改进您的PollingTimer
。谢谢您的回答,但我尝试了它,但它不起作用。
timer.Stop();
`System.Threading.Timer myTimer = new System.Threading.Timer((e) =>
{
liveScore(); //your function call
}, null,
TimeSpan.FromSeconds(0), //start immediately
TimeSpan.FromSeconds(5)); //execute every 5 secs`