想要将带有AFNetworking的json的NSArray或NSDictory从iOS传递到PHP WebServer
NSDictionary *courseList = ........;
NSString* name = @"UserName";
NSString* password = @"Password";
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
name, @"UserName",
password, @"Password",
courseList, @"courseList",
nil];
NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:SERVER_URL]];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client setDefaultHeader:@"Accept" value:@"application/json"];
[client setDefaultHeader:@"Accept-Charset" value:@"utf-8"];
[client postPath:URL
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id json) {
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}
];
最佳答案
NSDictionary * postDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"value1", @"value2", nil]
forKeys:[NSArray arrayWithObjects:@"key1", @"key2", nil]];
NSError * error = nil;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:postDictionary options:NSJSONReadingMutableContainers error:&error];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"your_webservice_post_url"]];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:jsonData];
NSURLConnection * myConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];