网络解析html

  // Request: My API (http://watch-cdn.idailywatch.com/api/list/cover/zh-hans?page=1&ver=iphone&app_ver=8)
    
    NSURL *URL = [NSURL URLWithString:@"http://watch-cdn.idailywatch.com/api/list/cover/zh-hans?page=1&ver=iphone&app_ver=8"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
    request.HTTPMethod = @"GET";
    request.timeoutInterval = 30;
    
    // Request Operation
    
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];//自动对返回的数据进行json解析
    
    // Progress & Completion blocks
    
    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        //NSLog(@"Received %lld of %lld bytes", totalBytesRead, totalBytesExpectedToRead);
    }];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success: Status Code %ld", (long)operation.response.statusCode);
        NSLog(@"%@", responseObject);
        

        //数据处理
        NSString *sourceString = [[responseObject firstObject] objectForKey:@"source"];
        NSString *titleString = [[responseObject firstObject] objectForKey:@"title"];
        NSString *pubdateString = [[responseObject firstObject] objectForKey:@"pubdate"];
        NSString *authorString = [[responseObject firstObject] objectForKey:@"author"];
        NSString *contentString = [[responseObject firstObject] objectForKey:@"content"];
        
        NSArray *newsArray = [[responseObject firstObject] objectForKey:@"news"];
        NSMutableString *newsString = [[NSMutableString alloc] initWithString:@"<ul class=\"relatedNews\"></ul>"];
        for (NSDictionary *temDic in newsArray) {
            [newsString insertString:[NSString stringWithFormat:@"<li><a>%@</a></li>", [temDic objectForKey:@"title"]] atIndex:newsString.length-5];
        }
        
        NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"entry" ofType:@"html"];
        NSMutableString *htmlString = [NSMutableString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%@", htmlString);
        
        NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"entry" ofType:@"css"];
        NSString *cssString = [NSString stringWithContentsOfFile:cssPath encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%@", cssString);
        
        [htmlString replaceOccurrencesOfString:@"%added_styles%" withString:cssString options:NSLiteralSearch range:NSMakeRange(0, [htmlString length])];
        [htmlString replaceOccurrencesOfString:@"%source%" withString:sourceString options:NSLiteralSearch range:NSMakeRange(0, [htmlString length])];
        [htmlString replaceOccurrencesOfString:@"%title%" withString:titleString options:NSLiteralSearch range:NSMakeRange(0, [htmlString length])];
        [htmlString replaceOccurrencesOfString:@"%pubdate%" withString:pubdateString options:NSLiteralSearch range:NSMakeRange(0, [htmlString length])];
        [htmlString replaceOccurrencesOfString:@"%author%" withString:authorString options:NSLiteralSearch range:NSMakeRange(0, [htmlString length])];
        [htmlString replaceOccurrencesOfString:@"%content%" withString:contentString options:NSLiteralSearch range:NSMakeRange(0, [htmlString length])];
        [htmlString replaceOccurrencesOfString:@"%font_size%" withString:@"font_size_2" options:NSLiteralSearch range:NSMakeRange(0, [htmlString length])];
        [htmlString replaceOccurrencesOfString:@"%relatedNews%" withString:newsString options:NSLiteralSearch range:NSMakeRange(0, [htmlString length])];
        
        NSLog(@"%@", htmlString);
                
        
//        [[responseObject firstObject] objectForKey:@"content"];
        
        [_myWebView loadHTMLString:htmlString baseURL:nil];
//        [_myWebView stringByEvaluatingJavaScriptFromString:<#(NSString *)#>]
 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error.localizedDescription);
    }];
    
    // Connection
    
    [operation start];
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值