【问题标题】:Lexical error when posting object using RestKit 0.2使用 RestKit 0.2 发布对象时出现词法错误
【发布时间】:2013-02-18 08:29:02
【问题描述】:

我在使用 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 将 {"title":"Foobar"} 等原始 json 数据发布到 url,没有任何问题。

有什么想法吗?

【问题讨论】:

  • 我也有这个问题,我将 Content-Type 更改为 text/plain 然后它工作正常
  • 好的,很酷。这样做的代码是什么?我试过 [client setDefaultHeader:@"Content-Type" value:@"text/plain"],但 request.header 仍然显示 "Content-Type" = "application/x-www-form-urlencoded; charset=utf- 8";
  • 我不知道 ios。我在 GWT 中使用了休息

标签: ios objective-c json restkit


【解决方案1】:

Blake Watters 已回复 google group of restkit

接下来,这是我的实现:

  1. 来自 RKManagedObjectRequestOperation 的子类,如果您是 ManagedObject。

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]];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多