Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

iphone - How to wait response and parse XML done in afnetworking iOS

I want to wait until server reponse and parse XML done, then call another function. How can i do that? I used this code to send request to server and use NSXMLParser to parse XML response.

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

Please give me any suggestion. Thanks much

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to make your request synchronous.

refer code something like:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...