1

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

2 に答える 2

1

これを試して

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:BASE_URL]];
[objectManager setRequestSerializationMIMEType:RKMIMETypeJSON];
于 2014-04-11T20:35:38.203 に答える
0

クラスRKNSJSONSerializationを登録する必要があります。以下のコードをメソッドに含めてください。

[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/plain"];
于 2013-10-07T11:30:11.300 に答える