【发布时间】: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