AFNetworking AFHTTPRequestOperation responseObject转换为NSXMLParser。解析器不包含所有http信息

时间:2014-11-25 16:18:24

标签: nsxmlparser afnetworking-2

我有一个关于AFNetworking获取HTML网页的问题。

我有一个网址:

NSString *url = @"http://www.w3schools.com/";

我也有一个方法:

- (void)AFNReturnXMLString:(NSString *)string{

NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];

// Make sure to set the responseSerializer correctly
op.responseSerializer = [AFXMLParserResponseSerializer serializer];
op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    // Get XMLParser Object
    NSXMLParser *XMLParser = (NSXMLParser *)responseObject;
    [XMLParser setShouldProcessNamespaces:YES];
    NSLog(@"%@", XMLParser);


    // Leave these commented for now (you first need to add the delegate methods)
    XMLParser.delegate = self;
    [XMLParser parse];


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error Fucking: %@", error);
}];
[op start];}

我已经使用了NSXMLParser委托。当我运行该方法时,变量&#34; qName&#34;仅显示根<html><head>标记,但不显示<body><body>的子节点。

我在块中使用了以下方法而不是NSXMLParser对象:

// Get HTML String
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);

该字符串显示所有页面内容。

以前有人遇到过这样的问题吗?

0 个答案:

没有答案