如何等待响应和parsing在networkingiOS中完成的XML

我想等到服务器响应并parsingXML完成,然后调用另一个函数。 我怎样才能做到这一点? 我用这个代码发送请求到服务器,并使用NSXMLParser来parsingXML响应。

NSURL *url1 = [NSURL URLWithString:@"linkserver"]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL: url1] ; NSDictionary *params1 = @{ @"a" : vd; @"b" : @"all" }; NSMutableURLRequest *afRequest = [httpClient requestWithMethod:@"GET" path:nil parameters:params1] ; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success"); NSString * parsexmlinput = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; [self parseXMLFile:parsexmlinput];// parse xml [self getItemFromStatus];// wait to call another function at here??? } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error: %@", error); } ]; [httpClient enqueueHTTPRequestOperation:operation]; } 

请给我任何build议。 非常感谢

你必须使你的请求同步

参考代码如下所示:

  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.twitter.com/1.1/friends/ids.json?"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10]; [request setHTTPMethod: @"GET"]; NSError *requestError; NSURLResponse *urlResponse = nil; NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError]; 

使用AFnetworking检查本教程Ray Wenderlich。

使用块和callback

 - (IBAction)xmlTapped:(id)sender{ NSString *weatherUrl = [NSString stringWithFormat:@"%@weather.php?format=xml",BaseURLString]; NSURL *url = [NSURL URLWithString:weatherUrl]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) { XMLParser.delegate = self; [XMLParser setShouldProcessNamespaces:YES]; [XMLParser parse]; } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) { UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather" message:[NSString stringWithFormat:@"%@",error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [av show]; }]; [operation start]; } 

你可以用同步的方式做…

 NSURLResponse *response = nil; NSError *error = nil; NSURL *url = [NSURL URLWithString:urlStr]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; httpClient.parameterEncoding = AFFormURLParameterEncoding; NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:[url path] parameters:params]; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; NSString * parsexmlinput = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; if(error) { errorCallback(error, nil); } else { //parse the xml here } 

要么

你可以通过在操作队列中添加[operation waitUntilFinished]来实现。AFNetworking 可以同步返回数据吗?

要么

编辑 :如果你不想使用AFNetworking library.I喜欢这种方式。

  NSString *action_Post =[[NSString alloc] initWithFormat:@"authToken=%@",theMutableString]; NSURL *action_Url =[NSURL URLWithString:@"ur url here"]; NSData *action_PostData = [action_Post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *action_postLength = [NSString stringWithFormat:@"%d", [action_PostData length]];//your parameter to be posted here NSMutableURLRequest *action_Request = [[NSMutableURLRequest alloc] init]; [action_Request setURL:action_Url]; [action_Request setHTTPMethod:@"POST"]; [action_Request setValue:action_postLength forHTTPHeaderField:@"Content-Length"]; [action_Request setValue:@"application/xml" forHTTPHeaderField:@"Accept"]; //[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [action_Request setHTTPBody:action_PostData]; NSURLResponse *action_response; NSData *action_Result = [NSURLConnection sendSynchronousRequest:action_Request returningResponse:&action_response error:&error]; if (!action_Result) { NSLog(@"Error"); } else { //Parse your xml here //call ur function } 

根据你的GET / PUT方法修改它。但是我build议你去使用POST方法,因为它被认为是安全的。