1

我是 iPhone 编程的新手。

我有一个NSMutableArray我想POST将该阵列添加到服务器php文件并更新我的服务器xml文件。

提前致谢。

4

1 回答 1

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

这可能会帮助你

于 2012-07-02T11:34:48.523 回答