0

我尝试将 AFNetworking 2.0 与 XML Web 服务调用集成,但我不知道我做错了什么。

首先,我通过扩展AFHTTPRequestOperationManager创建了一个共享客户端:

+ (instancetype)sharedClient {
static AFAppDotNetAPIClient *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    _sharedClient = [[AFAppDotNetAPIClient alloc] initWithBaseURL:[NSURL URLWithString:AFAppDotNetAPIBaseURLString]];
    _sharedClient.responseSerializer = [AFXMLParserResponseSerializer new];
});

return _sharedClient;

}

然后,我尝试使用 NSXMLParseDelegate 协议解析结果,但我无法获取任何数据。链接有效。

+ (AFHTTPRequestOperation *)globalTimelinePostsWithBlock {
 return [[AFAppDotNetAPIClient sharedClient] GET:@"http://www.enetpulse.com/wp-content/uploads/sample_xml_feed_enetpulse_soccer.xml" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
     NSXMLParser* parser = [[NSXMLParser alloc]initWithData:operation.responseData];

     [parser setDelegate:(id<NSXMLParserDelegate>)self];
     BOOL result = [responseObject parse];

} failure:nil];}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict  {
NSLog(@"found");

} ...

有谁知道我做错了什么?

4

1 回答 1

2

这个答案可能会迟到..

你为什么不试试这样的东西..

NSURL *urlLink = [[NSURL alloc] initWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:urlLink];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFXMLParserResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSXMLParser *XMLParser = (NSXMLParser*)responseObject;
        NHXmlParser *myParser = [[NHXmlParser alloc] init];
        XMLParser.delegate = myParser;
        [XMLParser setShouldProcessNamespaces:YES];
        [XMLParser parse];
        myParser.delegate = self;

    } failure:^(AFHTTPRequestOperation *request, NSError *error){
        NSLog(@"Error:------>%@", [error description]);
    }];
}

这对我来说很好。

于 2014-01-02T10:16:22.320 回答