RestKit 处理不同的 HTTP 状态代码

发布于 2024-12-08 08:42:05 字数 992 浏览 1 评论 0原文

我刚刚开始为我正在构建的 iOS 应用程序尝试 RestKit。我通常使用 ASIHttpRequest,但我想测试 RestKit 主要是为了它在 JSON 和 CoreData 之间的对象映射。 RestKit 有一些很棒的东西,但我遇到了一个问题,这确实让人感觉它有缺陷,除非我做错了什么或者错过了一些东西。我希望这里有人能指导我。

我正在使用 RKObjectLoader 来进行异步 &同步对 REST API 的调用。我的服务旨在发送回正确的 HTTP 状态代码以及某种描述,401 是 API 需要经过身份验证的用户时的示例。

我的问题是,如果我收到 401 错误,RestKit 将停止正常运行。 RKResponse 对象的状态代码为 0,即使其中包含有效负载。我很确定这归因于 NSURLConnection 对 HTTP 状态的糟糕处理,但我希望 RestKit 能够以某种方式解决这个问题。特别是因为 RKResponse 类有相当多的包装函数来确定响应的状态代码(isOK、isCreated、isForbidden、isUnauthorized 等)。

相比之下,ASIHttpRequest 不使用 NSURLConnection,而是使用较低级别的 CFNetwork 代码。 ASIHttpRequest 允许我准确地查看通过 HTTP 返回的内容,而不会发送剩余的错误和错误。正确的。

问题是,我做错了什么,还是这是 RestKit 的预期行为?有人成功拨打 [RKResponse isAuthenticated] 吗?尽管对我来说还没有定论,但在这方面以异步模式和同步模式运行有什么区别吗?我确实在某处读到,在同步模式下运行的 NSURLConnection 的行为会有所不同,即使底层代码只是调用异步操作。这是否与我使用 RKObjectLoader 而不仅仅是 RKRequest 有更多关系?也许有效负载无法映射到模型的事实会引起愤怒,但似乎代码在 RKRequest.sendSynchronously 中提前中断,在映射实际发生之前。

底线是我的代码需要能够自由读取 HTTP 状态代码。任何指导将不胜感激。

海德尔

I've just started to try out RestKit for an iOS app i'm building. I normally use ASIHttpRequest, but I want to test out RestKit mostly for its object mapping between JSON and CoreData. There are some great things about RestKit, but I've run into an issue that really makes it feel deficient, unless I'm doing something wrong or have missed something. I hope someone here can guide me on that.

I'm using RKObjectLoader to make async & sync calls to a REST API. My service is designed to send back proper HTTP status codes, along with some sort of description, a 401 being an example of when the API needs an authenticated user.

My problem is that RestKit stops acting normally if i get a 401 error back. The RKResponse object has a status code of 0, even though it has a payload in it. I'm pretty sure this comes down to NSURLConnection's poor handling of HTTP statuses, but I would expect RestKit to wrap around this somehow. Especially since the RKResponse class has quite a few wrapper functions to determine the status code of the response (isOK, isCreated, isForbidden, isUnauthorized, etc.).

In comparison, ASIHttpRequest doesn't use NSURLConnection, but instead uses the lower level CFNetwork code. ASIHttpRequest allows me to see exactly what came back over HTTP without sending out errors left & right.

Question is, am I doing something wrong, or is this the expected behavior out of RestKit? Has anyone successfully been able to make a calls to [RKResponse isAuthenticated]? Although its inconclusive to me, is there any difference between running in async and sync mode in this regard. I did read somewhere that NSURLConnection run in sync mode will act a bit differently, even though the underlying code is just calling the async operations. Does this have more to do with me using RKObjectLoader as opposed to just RKRequest? Perhaps the fact that the payload can't map to a model causes anger, but it seems that the code is breaking earlier within RKRequest.sendSynchronously, prior to when mapping actually takes place.

Bottom line is my code needs to be able to freely read HTTP status codes. Any guidance would be most appreciated.

Haider

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌味 2024-12-15 08:42:05

RestKit 0.20.x 的常用方法是子类化 RKObjectRequestOperation。
我写了一篇关于这个问题的博客文章,可以在这里找到:
http://blog.higgsboson.tk/2013/ 09/03/global-request-management-with-restkit/

The common way for RestKit 0.20.x is to subclass RKObjectRequestOperation.
I wrote a blog article about this problem which can be found here:
http://blog.higgsboson.tk/2013/09/03/global-request-management-with-restkit/

千纸鹤带着心事 2024-12-15 08:42:05

请参阅 http://groups.google.com/group/restkit/msg/839b84452f4b3e26

“...当身份验证失败时,身份验证质询将被取消,从而有效地使请求无效。”

更新:

RestKit 已经为此包含了一个委托方法:

(void)request:(RKRequest *)request didFailAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;

在 HTTP 基本身份验证失败时触发

(void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error

,因此我们可以使用它来代替。

See http://groups.google.com/group/restkit/msg/839b84452f4b3e26

"... when authentication fails, the authentication challenge gets cancelled and that effectively voids the request."

UPDATE:

RestKit already includes a delegate method for this:

(void)request:(RKRequest *)request didFailAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;

Triggers before

(void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error

When HTTP Basic authentication fails, so we can use this instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文