2012-07-09 147 views
0

我已经尝试了很多方法,但我仍然在服务器端获得一个空数组。我想将xml数据发布到服务器端php文件

这里是我的代码:

NSString *urlString = [[NSString alloc] initWithFormat:@"%@/test.php", APP_WEBSITE_URL];  
NSURL *requestURL = [NSURL URLWithString:urlString]; 
NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:requestURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 

[requestObj setHTTPMethod:@"POST"];    
[requestObj setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"]; 

NSInteger tmpCount = [strawPollDetailItem.selectionsArray count]; 

NSMutableData *postBody = [NSMutableData data]; 

[postBody appendData:[[NSString stringWithFormat:@"<SUBMITTED>"] dataUsingEncoding:NSUTF8StringEncoding]]; 
for (int i=0; i<tmpCount; i++) { 

    NSInteger selectedID = [strawPollDetailItem.selectionsArray objectAtIndex:i]; 

    [postBody appendData:[[NSString stringWithFormat:@"<OPTIONID>%@</OPTIONID>", selectedID] dataUsingEncoding:NSUTF8StringEncoding]]; 
} 

[postBody appendData:[[NSString stringWithFormat:@"</SUBMITTED>"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[requestObj setHTTPBody:postBody]; 
[[NSURLConnection alloc] initWithRequest:requestObj delegate:self]; 
+3

你为什么不尝试做自己的一些工作,而不是采取了答案[别人给你(http://stackoverflow.com/questions/11394933/i-am-try-to-post-the-xml-data-to-server-side-php-file)并重新发布,请求某人完成下一部分工作。花时间学习东西。我们在这里帮助你,而不是为你做你的工作。 – 2012-07-09 12:30:52

回答

0
NSURL * serviceUrl = [NSURL URLWithString:@"weblink"]; 
    NSMutableURLRequest * serviceRequest = [NSMutableURLRequest requestWithURL:serviceUrl]; 
    [serviceRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-type"]; 
    //[serviceRequest setValue:txtTitle.text forHTTPHeaderField:@"title"]; 
    [serviceRequest setHTTPMethod:@"POST"]; 
    [serviceRequest setHTTPBody:[xmlString dataUsingEncoding:NSASCIIStringEncoding]]; 
    [serviceRequest setValue:[NSString stringWithFormat:@"%d",[xmlString length]] forHTTPHeaderField:@"Content-Length"]; 
    NSURLResponse * serviceResponse; 
    NSError * serviceError; 

    NSData *responseData = [NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&serviceResponse error:&serviceError]; 
    NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease]; 
+0

在PHP文件中应该使用哪个变量名来获取xml字符串? – shervin 2012-07-17 11:50:37

相关问题