JSON形式のデータをRESTAPIに送信しようとしています。パラメータを送信すると、Webサービスはデータを返しませんが、代わりに次のエラーを返します。JSONの解析中にエラーが発生しました:Error Domain = NSCocoaErrorDomain Code = 3840 "操作を完了できませんでした。(Cocoaエラー3840)"(JSONテキストは配列またはオブジェクト、およびフラグメントを設定できないようにするオプションで開始しないでください。)
これは、Webサービスがパラメータを読み取れないためです。しかし、このパラメーターをURLに直接追加すると、正しい結果が返されます。例:http:// localhost:8080 / de.vogella.jersey.final / rest / notes / 63056
パラメータを送信するためのコードは次のとおりです。
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost:8080/de.vogella.jersey.final/rest/notes/"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSString *postString = @"{\"notes\":\"63056\"}";
NSLog(@"Request: %@", postString);
// NSString *postString = @"";
[request setValue:[NSString stringWithFormat:@"%d",[postString length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSLog(@"Return Data%@",returnData);
//Getting the task types from the JSON array received
NSMutableArray *jsonArray =[NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&error];
NSLog(@"jsonArray is %@",jsonArray);
taskTypes =[[NSMutableArray alloc]init ];
if (!jsonArray) {
NSLog(@"Error parsing JSON: %@",error);
} else {
for(NSDictionary *taskType in jsonArray) {
[taskTypes addObject:[taskType objectForKey:@"TaskName"]];
}
}
助言がありますか?