我应该如何在 Windows Phone 7 上使用 RestSharp 实现 ExecuteAsync? [英] How should I implement ExecuteAsync with RestSharp on Windows Phone 7?

查看:22
本文介绍了我应该如何在 Windows Phone 7 上使用 RestSharp 实现 ExecuteAsync?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 RestSharp GitHub wiki 上的文档来实现对我的 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.

目前我的 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 命令应该包含什么.我不能使用 return response.Data 因为我被警告了:

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?

推荐答案

你的代码应该是这样的:

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);
        });
    }
}

我添加了两个方法,所以你可以检查你想要什么(来自响应正文的字符串内容,或者这里由 MyClass 表示的反序列化类)

I added two methods, so you can check what you want (string content from the response body, or a deserialized class represented here by MyClass)

这篇关于我应该如何在 Windows Phone 7 上使用 RestSharp 实现 ExecuteAsync?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆