1

我对 Restkit 还很陌生,但到目前为止,它使用 0.20.3 版本对我来说工作得很好,可以满足我的大部分网络需求。

我正在使用 WCF webhttp 绑定用 c# 编写的基于 json 的 API,此时值得一提的是,我完全无法控制此 API,也无法更改返回的 json 的格式,我需要使用我所拥有的。

问题是,当 API 返回一个简单类型(如 int、double 或 string)作为响应时,json 响应完全裸露,如下所示。

字符串响应

"hello world"

内部响应

2342524

这两个示例响应的内容类型均为 application/json

我尝试使用带有 restkit 的 API 端点,该端点通过客户编号获取客户订单的计数。请求的代码如下,我期待一个 NSNumber 作为响应,但它生成一个错误作为它的原始未包装类型,我无法为此提供映射。

 [manager getObject:nil
                  path:@"/service/http/CountOrders?CustomerId=324534413"
            parameters:nil
               success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){
                   RKLogInfo(@"order count: %@",mappingResult);

               } failure:^(RKObjectRequestOperation *operation, NSError *error) {

                   RKLogError(@"Operation failed with error: %@", error);
               }];

我回来的错误是

restkit.network:RKResponseMapperOperation.m:317 Failed to parse response data: Loaded an unprocessable response (200) with content type 'application/json'
restkit.network:RKObjectRequestOperation.m:213 GET 'http://CUSTOMERDOMAIN/service/http/CountOrders?CustomerId=324534413' 
(200 OK / 0 objects) 
[request=0.2263s mapping=0.0000s total=0.2386s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1017 "Loaded an unprocessable response (200) with content type 'application/json'" 

UserInfo=0x8e1a660 {NSErrorFailingURLKey=http://CUSTOMERDOMAIN/service/http/CountOrders?CustomerId=324534413, NSUnderlyingError=0x8e1b1a0 
"The operation couldn’t be completed. (Cocoa error 3840.)", 
NSLocalizedDescription=Loaded an unprocessable response (200) with content type 'application/json'}
hj

有什么方法可以解析对 NSNumber 的响应以涵盖这种边缘情况?我认为如果可能的话,自定义反序列化处理程序可能是要走的路,但正如我所说,我是 restkit 的新手,基本上是在寻找正确的方向。

4

1 回答 1

0

自定义序列化程序可以工作,但一种简单的方法可能是跳过 RestKit 以获取具有“简单”响应的请求,并改用底层 AFNetworking 类。然后你不需要担心序列化,你可以快速强制响应值。

于 2013-10-13T09:59:52.327 回答