我应该如何在Windows Phone 7实施ExecuteAsync与RestSharp? [英] How should I implement ExecuteAsync with RestSharp on Windows Phone 7?
问题描述
我试图使用文档的 RestSharp GitHub的维基落实到我的REST调用API服务,但我在遇到特别是ExecuteAsync方法的问题。
I'm attempting to use the documentation on the RestSharp GitHub wiki to implement calls to my REST API service but I'm having an issue with the ExecuteAsync method in particular.
目前我的code看起来像这样的API类:
Currently my code looks like this for the API class:
public class HarooApi
{
const string BaseUrl = "https://domain.here";
readonly string _accountSid;
readonly string _secretKey;
public HarooApi(string accountSid, string secretKey)
{
_accountSid = accountSid;
_secretKey = secretKey;
}
public T Execute<T>(RestRequest request) where T : new()
{
var client = new RestClient();
client.BaseUrl = BaseUrl;
client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey);
request.AddParameter("AccountSid", _accountSid, ParameterType.UrlSegment);
client.ExecuteAsync<T>(request, (response) =>
{
return response.Data;
});
}
}
我知道这个从什么是GitHub的页面上偏离略有但我使用这跟WP7并相信例子是C#因此ExecuteAsync方法的使用。
I'm aware this slightly deviates from what is on the GitHub page but I'm using this with WP7 and believe the example is for C# hence the usage of the ExecuteAsync method.
我的问题是与ExecuteAsync命令应该包含的内容。我不能使用返回response.Data code>,因为我告诫:
My problem is with what the ExecuteAsync command should contain. I can't use return response.Data
because I'm warned:
'System.Action<RestSharp.RestResponse<T>,RestSharp.RestRequestAsyncHandle>' returns void, a return keyword must not be followed by an object expression
有没有人对如何解决这个任何见解或可协助教程?
Does anyone have any insight on how to fix this or a tutorial that may assist?
推荐答案
您code应该是这个样子:
Your code should look something like this:
public class HarooApi
{
const string BaseUrl = "https://domain.here";
readonly string _accountSid;
readonly string _secretKey;
public HarooApi(string accountSid, string secretKey)
{
_accountSid = accountSid;
_secretKey = secretKey;
}
public void ExecuteAndGetContent(RestRequest request, Action<string> callback)
{
var client = new RestClient();
client.BaseUrl = BaseUrl;
client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey);
request.AddParameter("AccountSid", _accountSid, ParameterType.UrlSegment);
client.ExecuteAsync(request, response =>
{
callback(response.Content);
});
}
public void ExecuteAndGetMyClass(RestRequest request, Action<MyClass> callback)
{
var client = new RestClient();
client.BaseUrl = BaseUrl;
client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey);
request.AddParameter("AccountSid", _accountSid, ParameterType.UrlSegment);
client.ExecuteAsync<MyClass>(request, (response) =>
{
callback(response.Data);
});
}
}
我添加了两个方法,所以你可以检查你想要什么(从响应体,或者通过这里psented反序列化类再$ P $字符串内容 MyClass的
)
这篇关于我应该如何在Windows Phone 7实施ExecuteAsync与RestSharp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!