iOS - ASIHTTPRequest - 尝试通过responseString进行解析

时间:2012-04-09 01:15:20

标签: ios asihttprequest

我花了大约16个小时研究和尝试不同的代码更改,但无法弄清楚这个。我有一个使用ASIHTTPrequest消费网站的iOS应用程序:

-(void)refresh{
    NSURL *url = [NSURL URLWithString@"http://undignified.podbean.com/"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request startSynchronous];
    NSString *response = [request responseString];
    NSLog(@"%@",response];
 }

以上代码返回网站源代码并通过NSLog将其吐出到控制台。我想要实现的目标是搜索以'.mp3结尾的URL的'responseString',将它们加载到数组中,最后将mp3 URL加载到UITableView中。

总结:

  1. 使用ASIHTTPRequest消耗网站数据
  2. 尝试在responseString中搜索所有具有* .mp3扩展名的链接并将其加载到数组中。
  3. 将解析后的链接添加到UITableView。
  4. 我认为在这个交界处,我已经尝试了太多的事情来做出任何合理的判断。任何对正确方向的建议或推动都会非常感激。

    以下是响应示例(HTML)。请注意,这只是一个片段,因为整个HTML源代码都很大,但这包括mp3文件的一部分:

    a href="http://undignified.podbean.com/mf/web/ih2x8r/UndignifiedShow01.mp3"         target="new"><img src="http://www.podbean.com/wp-content/plugins/podpress/images/audio_mp3_button.png" border="0" align="top" class="podPress_imgicon" alt="icon for podbean" /></a> &nbsp;Standard Podcasts [00:40:24m]: <a href="javascript:void(null);" onclick="podPressShowHidePlayerDiv('podPressPlayerSpace_2649518', 'mp3Player_2649518_0', '300:30', 'http://undignified.podbean.com/mf/play/ih2x8r/UndignifiedShow01.mp3'); return false;"><span id="podPressPlayerSpace_2649518_label_mp3Player_2649518_0">Play Now</span></a> | <a href="javascript:void(null);" onclick="window.open ('http://undignified.podbean.com/wp-content/plugins/podpress/podpress_backend.php?podPressPlayerAutoPlay=yes&amp;standalone=yes&amp;action=showplayer&amp;pbid=0&amp;b=458211&amp;id=2649518&amp;filename=http://undignified.podbean.com/mf/play/ih2x8r/UndignifiedShow01.mp3', 'podPressPlayer', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=660,height=360'); return false;">Play in Popup</a> | <a href="http://www.podbean.com/podcast-download?b=458211&f=http://undignified.podbean.com/mf/web/ih2x8r/UndignifiedShow01.mp3" target="26108">Download</a> | <a href="http://www.podbean.com/podcast-players?b=458211&p=2649518&f=http://undignified.podbean.com/mf/play/ih2x8r/UndignifiedShow01.mp3" target="38148">Embeddable Player</a> | <a>Hits (214)</a><br/
    

3 个答案:

答案 0 :(得分:0)

我会分两步完成:

  1. 获取所有href值

  2. 使用正则表达式检查以.mp3

  3. 结尾的有效网址 顺便说一下,我认为ASIHTTPRequest已经过时了。如果你只使用它来获取HTML,我建议你研究一下iOS框架中的构建方法来做到这一点。

答案 1 :(得分:0)

使用requestDidReceiveResponseHeadersSelector并从标题中获取文件名,尝试从NSDictionary标题中打印所有键和值。

答案 2 :(得分:0)

我真的不喜欢那些说“就是不要那样做。”的答案。但是......就是不要这样做。

可能从您发布的地址中提取html中mp3的所有链接。但这几乎总是处理事情的错误方式,这种情况也不例外。

基本上你正在尝试做的是创建一个podcaster客户端。您应该先考虑一下其他人如何处理此类用例。通常,播客会有一个关联的RSS Feed,它会准确概述您要查找的数据,您的播客也不例外。如果您只是导航到问题中提供的链接,然后在页面中查找“subscribe to podcast”或“RSS”,他们就会找到导致RSS Feed的链接: http://undignified.podbean.com/feed/。此地址会显示XML,其中包含播客的item个。

与原始地址返回的文档不同,此文档有效XML,表示可以使用NSXMLParser进行解析。 NSXMLParser是非常强大的&amp;灵活。但有点难以入手。以下是NSXMLParser子类的一些示例代码,它充当了它自己的委托。

<强> UnDigParser.h

#import <Foundation/Foundation.h>
@interface UnDigParser : NSXMLParser <NSXMLParserDelegate>
@property (readonly) NSArray *links;
@end

<强> UnDigParser.m

#import "UnDigParser.h"
@implementation UnDigParser{
    NSMutableArray *_links;
}
@synthesize links = _links;
-(void)parserDidStartDocument:(NSXMLParser *)parser{
    _links = [[NSMutableArray alloc] init];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    if ([elementName isEqualToString:@"enclosure"]){
        NSString *link = [attributeDict objectForKey:@"url"];
        if (link){
            [_links addObject:link];
        }
    }
}
-(BOOL)parse{
    self.delegate = self;
    return [super parse];
}
@end

此代码可以像这样进行测试:

dispatch_async(dispatch_get_global_queue(0, 0), ^{
    NSURL *url = [NSURL URLWithString:@"http://undignified.podbean.com/feed/"];
    // This is a sync call thus the background thread
    UnDigParser *parser = [[UnDigParser alloc] initWithContentsOfURL:url];
    [parser parse];
    NSLog(@"links:%@",parser.links);
});

记录的输出是:

links:(
    "http://undignified.podbean.com/mf/feed/cfdayc/UndignifiedShow03.mp3",
    "http://undignified.podbean.com/mf/feed/wbbpjw/UndignifiedShow02Final.mp3",
    "http://undignified.podbean.com/mf/feed/ih2x8r/UndignifiedShow01.mp3",
    "http://undignified.podbean.com/mf/feed/t4x54d/UndignifiedShow00.mp3"
)

这应该足以让你开始。

相关问题