我想将数据发布到JSON webservice。我可以成功,如果我这样做:NSURLRequest POST导致空白提交
curl -d "project[name]=hi&project[description]=yes" http://mypath.com/projects.json
我试图用这样的代码来完成它:
NSError *error = nil;
NSDictionary *newProject = [NSDictionary dictionaryWithObjectsAndKeys:self.nameField.text, @"name", self.descField.text, @"description", nil];
NSLog(@"%@", self.descField.text);
NSData *newData = [NSJSONSerialization dataWithJSONObject:newProject options:kNilOptions error:&error];
NSMutableURLRequest *url = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://mypath.com/projects.json"]];
[url setHTTPBody:newData];
[url setHTTPMethod:@"POST"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:url delegate:self];
我请求创建一个新的条目,但该条目是空白名字和描述。我的NSLog在上面的代码产生适当的输出。
:另外,如果你想使用curl张贴JSON(如您的ObjC代码示例在做),你会做它像这样表格数据? – Perception 2012-04-20 22:19:13