【问题标题】:RestSharp in Xamarin for Android not giving any responseXamarin for Android 中的 RestSharp 没有给出任何响应
【发布时间】:2014-03-13 17:44:27
【问题描述】:

我目前正在尝试使用 Mono for android,目前我在以下 URL 安装了基于 Rest 的服务

http://localhost:8080//api/mobileclient?email=aakpenyi3%40yahoo.com&phone=80604456789

服务器是IIS8.0,服务是使用asp.net web API构建的。

当我使用 Curi 或任何基于插件的 Rest 客户端发出请求时。一切顺利,响应如期而至

不过我喜欢在移动客户端中使用它

我有以下代码使用 Xamarin for Android

    public class RequestBrokerClass
{
    const string baseUrl = "http://partnerens.com/api/mobileclient/";


    public T Execute<T>(RestRequest request) where T : new()
    {
        var client = new RestClient ();
        client.BaseUrl = baseUrl;
        var response = client.Execute<T> (request);
        if (response.ErrorException != null) {
            const string message = "Error contacting partnerens servers";
            var exception = new ApplicationException (message, response.ErrorException);
            throw exception;
        }
        return response.Data;

    }

}

和下面的摘录使用上面的代码。 (请注意,电子邮件已经过 URL 编码)。

public IEnumerable<PartnershipRecords> GetItemFromApi(string email, string phoneNumber)
    {

        var request = new RestRequest ();
        request.Method = Method.GET;
        request.AddParameter ("email", email);
        request.AddParameter ("phone", phoneNumber);
        var items = new RequestBrokerClass ().Execute<List<PartnershipRecords>> (request);
        return items;

    }

但是我的问题是,如果使用移动客户端进行响应,则始终为空,但如果使用 REST 客户端进行,则不会为空。

【问题讨论】:

  • 您排除了连接问题吗?尝试使用 'var response = new WebClient.DownloadString("localhost:8080//api/…);' 加载纯文本

标签: c# asp.net-web-api xamarin restsharp xamarin-studio


【解决方案1】:

好的,我已经解决了,这就是我所做的

我在 AndroidManifest.xml 中添加了以下内容

    <uses-permission android:name="android.permission.INTERNET" />

瞧,服务器响应适当

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多