繁体   English   中英

RestKit POST请求出错

[英]Error with RestKit POST Request

我正在尝试使用RestKit将POST请求中的JSON数据发送到服务器,但出现错误

Expected content type {(
    "application/x-www-form-urlencoded",
    "application/json"
)}, got text/plain

我写的代码是:

NSURL *url=[NSURL URLWithString:[NSString   stringWithFormat:@"%@%@",BASE_SERVICE_URL,TASK_SERVICE_URL]];
RKObjectMapping *mapping=[RKObjectMapping mappingForClass:[LoginData class]];
[mapping addAttributeMappingsFromDictionary:@{@"s":@"status",
                                              @"message":@"message"}];
RKResponseDescriptor *descriptor=[RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodPOST pathPattern:nil keyPath:@"" statusCodes:0];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSDictionary *dict=@{@"task":@"storeProfile",
                     @"fdate":@"01.01.2013",
                     @"tdate":@"19.08.2013",
                     @"keyword":@"taxi && cab",
                     @"AlertHours":@"120",
                     @"AlertDay":@"0",
                     @"AlertTimeStart":@"12:00",
                     @"AlertTimeEnd":@"18:00",
                     @"topic":@[
                             @{@"tname":@"Politics"},
                             @{@"tname":@"Economy"}
                             ],
                     @"publication":@[
                             @{@"pname":@"Vijay Times Online"},
                             @{@"pname":@"Leisure Opportunities"}
                             ]
                     };
NSError *error=nil;
NSData *jsonData=[NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error];
if (error) {
    NSLog(@"%@",error.localizedDescription);
    return;
}
[request setHTTPBody:jsonData];
operation=[[RKObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[descriptor]];
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    LoginData *data=[mappingResult.dictionary objectForKey:@""];
    NSLog(@"%@",data.message);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"ERROR:%@",error.localizedDescription);
}];
[operation start];

有人可以帮我弄清楚到底是什么问题吗? 我也尝试设置内容类型,如下所示:

[request addValue:@"application/application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

尝试这个

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:BASE_URL]];
[objectManager setRequestSerializationMIMEType:RKMIMETypeJSON];

您必须注册RKNSJSONSerialization类。 请在您的方法中包含以下代码。

[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/plain"];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM