-3

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

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

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

4

3 回答 3

1

查看本教程Ray Wenderlich使用 AFnetworking。

使用块和回调

- (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]; 
}
于 2013-08-21T07:58:23.877 回答
1

您必须使您的请求同步

参考代码如下:

 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];
于 2013-08-21T07:50:42.260 回答
0

您可以以同步方式进行...

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
   }

或者

你可以通过添加[操作等待UntilFinished]来实现它,在它被添加到操作队列之后。参考this==> AFNetworking可以同步返回数据(在一个块内)吗?

或者

编辑:如果您不想使用 AFNetworking 库。我更喜欢这种方式。

 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 方法修改它。但我建议您使用 POST 方法,因为它被称为安全方法。

于 2013-08-21T07:51:51.100 回答