我是iPhone编程新手。
我有一个NSMutableArray
我希望POST
该数组到服务器的php
文件并更新我服务器的xml
文件。
提前致谢。
答案 0 :(得分:1)
您可能需要此代码..
在.h中声明 NSURLConnection * postConnection 的引用,并将其作为属性并在.m文件中合成...
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"yourPhpUrl.php"]];
[request setHTTPMethod:@"POST"];
NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc] init];
[jsonDict setValue:yourArray forKey:@"userName"];
NSLog(@"JSON Dict: %@",jsonDict);//Check your array here...
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON String: %@",jsonString); //Check your post String here...
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: jsonData];
self.postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:true];
这可能会对你有所帮助