Xamarin PCL向REST服务发出请求并返回模型

时间:2014-02-05 18:12:35

标签: xamarin.android xamarin portable-class-library

因此,我建议我使用RestSharp来处理REST Web服务。我正在开发iOS和Android应用程序,我很乐意让PCL向服务发出请求并返回已解析的结果(例如,User对象数组)。

那么我如何在我的PCL中获取RestSharp,尝试过NuGet,组件不适用于PCL,严重的是只需下载源文件并将其复制到项目中,我希望保留一些依赖管理。

最佳做法是什么?我是以错误的角度看待这个问题吗?

4 个答案:

答案 0 :(得分:3)

RestSharp不支持PCL。我建议查看PortableRest,或者只使用HttpClient和Json.NET的组合。

答案 1 :(得分:1)

我使用依赖注入,所以我可以支持非PCL JSON解析器。我还计划尝试从组件存储中提供本机HttpClient包装器。通过使用非PCL代码,与Json.NET等相比,您将获得相当多的性能。

Link to source code

文本库具有序列化程序接口,Web具有IRestClient。

Modern HTTP Client from the component store.

答案 2 :(得分:1)

以下修改对我有用,如果对你有用,我会很高兴。 尝试在PCL中使用modernhttpclient。 inAndroid项目确保您拥有以下软件包。

  1. Microsoft.Bcl.Build
  2. Microsoft.Bcl
  3. Microsoft.Net.Http
  4. modernhttpclient
  5. 在所需权限下的应用程序清单中,以及下面的权限。

    1. ACCESS_NETWORK_STATE
    2. ACCESS_WIFI_STATE
    3. 互联网
    4. 理想情况下,当您尝试将Microsoft.Bcl添加到Android项目中时,目标是monoandroid,它会抛出错误,因此请尝试按上述顺序添加nuget参考。

答案 3 :(得分:0)

我开发了一个非常简单的REST客户端来轻松执行Http请求。你可以在我的Github repo上查看。 API非常简单:

await new Request<T>()
.SetHttpMethod(HttpMethod.[Post|Put|Get|Delete].Method) //Obligatory
.SetEndpoint("http://www.yourserver.com/profilepic/") //Obligatory
.SetJsonPayload(someJsonObject) //Optional if you're using Get or Delete, Obligatory if you're using Put or Post
.OnSuccess((serverResponse) => { 
   //Optional action triggered when you have a succesful 200 response from the server
  //serverResponse is of type T
})
.OnNoInternetConnection(() =>
{
    // Optional action triggered when you try to make a request without internet connetion
})
.OnRequestStarted(() =>
{
    // Optional action triggered always as soon as we start making the request i.e. very useful when
    // We want to start an UI related action such as showing a ProgressBar or a Spinner.
})
.OnRequestCompleted(() =>
{
    // Optional action triggered always when a request finishes, no matter if it finished successufully or
    // It failed. It's useful for when you need to finish some UI related action such as hiding a ProgressBar or
    // a Spinner.
})
.OnError((exception) =>
{
    // Optional action triggered always when something went wrong it can be caused by a server-side error, for
    // example a internal server error or for something in the callbacks, for example a NullPointerException.
})
.OnHttpError((httpErrorStatus) =>
{
    // Optional action triggered when something when sending a request, for example, the server returned a internal
    // server error, a bad request error, an unauthorize error, etc. The httpErrorStatus variable is the error code.
})
.OnBadRequest(() =>
{
    // Optional action triggered when the server returned a bad request error.
})
.OnUnauthorize(() =>
{
    // Optional action triggered when the server returned an unauthorize error.
})
.OnInternalServerError(() =>
{
    // Optional action triggered when the server returned an internal server error.
})
//AND THERE'S A LOT MORE OF CALLBACKS THAT YOU CAN HOOK OF, CHECK THE REQUEST CLASS TO MORE INFO.
.Start();