如何在目标c中将NSMutable数组发布到php文件

时间:2012-07-02 11:14:53

标签: php iphone xml nsmutablearray

我是iPhone编程新手。

我有一个NSMutableArray我希望POST该数组到服务器的php文件并更新我服务器的xml文件。

提前致谢。

1 个答案:

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

这可能会对你有所帮助