使用RestKit 0.2发布对象时出现词法错误

时间:2013-02-18 08:29:02

标签: ios objective-c json restkit

我在使用RestKit 0.2做一个简单的帖子时遇到了一些问题。这就是我的代码:

RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromArray:@[@"title"]];

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[Feed class] rootKeyPath:@""]

NSURL *baseURL = [NSURL URLWithString:@"http://some.url.com"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];

[client setDefaultHeader:@"Accept" value:RKMIMETypeJSON];

RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];

[objectManager addRequestDescriptor:requestDescriptor];

Feed *feed = [[Feed alloc] init];
feed.title = @"SomeTitle";

[objectManager postObject:feed path:/feeds parameters:nil success:nil failure:nil];

当我运行上面的代码时,我得到以下日志:

request.headers={
    Accept = "application/json";
    "Accept-Language" = "sv, en, nl, fr, de, ja, it, es, pt, pt-PT, da, fi, nb, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8";
    "Content-Type" = "application/x-www-form-urlencoded; charset=utf-8";
    "User-Agent" = "appName/1.2 (iPhone; iOS 5.1.1; Scale/2.00)";
}
request.body=[title]=Foobar
2013-02-13 12:05:55.450 appName[3672:330f] T restkit.network:RKHTTPRequestOperation.m:177 POST 'some.url.com' (400 Bad Request) [0.5037 s]:
response.headers={
    Connection = "keep-alive";
    "Content-Length" = 99;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Wed, 13 Feb 2013 11:05:55 GMT";
    "Set-Cookie" = "cookieTextHere; domain=.some.domain.com; path=/; expires=Wed, 27-Feb-2013 11:05:55 GMT; HttpOnly";
    "X-Request-Id" = xxe131e73e638733519c44xxa6224aa3f2xx;
}
response.body={"title":"JSON Parser Error","errors":
"lexical error: invalid string in json text. [title]=Foobar"}

REST服务器期望以JSON格式发布帖子,但是当我查看上面的日志时,我看到请求的内容类型设置为“application / x-www-form-urlencoded” - 这是否会导致问题?我尝试使用

更改此内容
[client setDefaultHeader:@"Content-Type" value:RKMIMETypeJSON];

......但那没用。

我已经使用www.hurl.it将原始的json数据(如{“title”:“Foobar”})发布到网址上,没有任何问题。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

Blake Watters已回复google group of restkit

接下来,这是我的实施:

  1. 来自RKManagedObjectRequestOperation的SubClass,如果是你的ManagedObject。
  2. EPObjectRequestOperation.h

    #import <RestKit/RestKit.h>
    
    @interface EPObjectRequestOperation : RKManagedObjectRequestOperation
    
    @end
    

    EPObjectRequestOperation.m

    #import "EPObjectRequestOperation.h"
    
    @interface EPObjectRequestOperation ()
    
    @property (nonatomic, strong, readwrite) NSError *error;
    @end
    
    @implementation EPObjectRequestOperation
    
    - (void) main
    {
        [super main];
        if([self.HTTPRequestOperation hasAcceptableStatusCode] && self.error.code == -1016) {//Expected content type
    
            self.error = nil;
        }
    }
    @end
    

    2.RKObjectManager上的注册子类

    [objectManager registerRequestOperationClass:[EPObjectRequestOperation class]];