2012-07-02 51 views

回答

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

可能這會幫助你

+0

這很好。而不是創建json對象。我們如何創建XML對象。 – shervin

+0

爲什麼你需要創建xml對象..? – Abhishek

+0

你能給我服務器端編碼嗎? – Rajneesh071