// 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];
网络解析html
最新推荐文章于 2024-02-11 21:22:44 发布