【发布时间】:2016-04-07 21:06:13
【问题描述】:
我正在使用 restsharp 从我正在做的 API 获取数据,我想创建一个名为“ApiInterface”的类,该类将在 Xamarin(Android、iOS、Windows...)中用于调用API。
这个类有它的 RestClient 和函数,可以从代码的任何部分调用,因为它是一个单例。
例如,我将有一个类似这样的 MainActivity.cs。 (调用我的 getData 函数并处理我收到的数据)。
Button buttonListaIntereses = FindViewById<Button> (Resource.Id.myButton);
buttonListaIntereses.Click += delegate {
ApiInterface.Instance.getData(response2=>
{
Intent displayMessage = new Intent(this, typeof(DisplayMessage));
//Put info in Extra for the new screen.
displayMessage.PutExtra("content", response2.Content);
StartActivity(displayMessage);
});
};
但在 APIInterface 中,我想获取常见的数据,例如 cookie。
public async void getData(Action <IRestResponse> onDone)
{
RestRequest request = new RestRequest("getData", Method.GET);
//Execute ASYNC the rest request
m_Client.ExecuteAsync (request, response =>
{
//Do my stuff with headers.
string lCookie = response.Headers.ToList().Find(x => x.Name == "Cookie").Value.ToString();
//Execute the OnDone
onDone();
});
}
我的问题是我不确定如何在 getData 中执行 OnDone 和/或如何调用 getData 函数。
谢谢!
【问题讨论】: