RestSharp on Response 不知道如何调用回调
RestSharp on Response dont know how to call callback
我正在使用 restsharp 从我正在做的 API 中获取数据,我想制作一个名为 "ApiInterface" 的 class,它将在 Xamarin 中使用( Android、iOS、Windows...) 调用 API.
这个 class 有它的 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);
});
};
但在 API 界面中,我想获取 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 and/or 如何调用 getData 函数。
谢谢!
Action 参数 onDone 采用 IRestReponse:
类型的参数
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 action with the received response
onDone(response);
});
}
我会放弃使用回调并利用 C# 的异步/等待功能
buttonListaIntereses.Click += async delegate {
var response = await ApiInterface.Instance.getData();
LaunchResponseActivity(response);
};
public void LaunchResponseActivity(IRestResponse response)
{
Intent displayMessage = new Intent(this, typeof(DisplayMessage));
//Put info in Extra for the new screen.
displayMessage.PutExtra("content", response.Content);
StartActivity(displayMessage);
}
public async Task<IRestResponse> getData()
{
RestRequest request = new RestRequest("getData", Method.GET);
var cancellationTokenSource = new CancellationTokenSource();
var restResponse = await client.ExecuteTaskAsync(request, cancellationTokenSource.Token);
//Do my stuff with headers.
string lCookie = restResponse.Headers.ToList().Find(x => x.Name == "Cookie").Value.ToString();
return restResponse;
}
我正在使用 restsharp 从我正在做的 API 中获取数据,我想制作一个名为 "ApiInterface" 的 class,它将在 Xamarin 中使用( Android、iOS、Windows...) 调用 API.
这个 class 有它的 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);
});
};
但在 API 界面中,我想获取 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 and/or 如何调用 getData 函数。
谢谢!
Action 参数 onDone 采用 IRestReponse:
类型的参数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 action with the received response
onDone(response);
});
}
我会放弃使用回调并利用 C# 的异步/等待功能
buttonListaIntereses.Click += async delegate {
var response = await ApiInterface.Instance.getData();
LaunchResponseActivity(response);
};
public void LaunchResponseActivity(IRestResponse response)
{
Intent displayMessage = new Intent(this, typeof(DisplayMessage));
//Put info in Extra for the new screen.
displayMessage.PutExtra("content", response.Content);
StartActivity(displayMessage);
}
public async Task<IRestResponse> getData()
{
RestRequest request = new RestRequest("getData", Method.GET);
var cancellationTokenSource = new CancellationTokenSource();
var restResponse = await client.ExecuteTaskAsync(request, cancellationTokenSource.Token);
//Do my stuff with headers.
string lCookie = restResponse.Headers.ToList().Find(x => x.Name == "Cookie").Value.ToString();
return restResponse;
}