ios 获取html中的json数据,[IOS]UIWebView实现保存页面和读取服务器端json数据
#import"ViewController.h"
@interfaceViewController()
@end
@implementation ViewController
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//請求網絡頁面
//? ? NSURL * url = [NSURL URLWithString:@"http://www.taobao.com"]; ? //一定要加http://
//? ? NSURLRequest * request = [NSURLRequest requestWithURL:url];
//? ? [self.webview loadRequest:request];
//
//
//? ? //html加載本地網頁
//? ? NSString * str = [[NSBundle mainBundle] pathForResource:@"百度圖片—全球最大中文圖片庫" ofType:@"html"];
//? ? str = [NSString stringWithContentsOfFile:str encoding:NSUTF8StringEncoding error:nil];
//? ? NSLog(@"%@",str);
//? ? [self.webview loadHTMLString:str baseURL:[[NSBundle mainBundle]bundleURL]];
//
self.label.text =@"正在請求數據";
//step1:請求地址
//保存頁面
//NSString * urlString = @"http://www.baidu.com";
//訪問服務器獲取json數據
NSString* urlString =@"http://www.weather.com.cn/data/cityinfo/101020100.html";
NSURL * url = [NSURLURLWithString:urlString];
//step2:實例化一個request
NSURLRequest* request = [NSURLRequestrequestWithURL:urlcachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:30.0];
//step3:創建鏈接
self.connection= [[NSURLConnectionalloc]initWithRequest:requestdelegate:self];
if(self.connection)
{
NSLog(@"創建鏈接成功");
}else{
NSLog(@"創建鏈接失敗");
}
[url release];
[urlString release];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[_webview release];
[_searchTextrelease];
[_label release];
[super dealloc];
}
//獲取數據
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//接受一個服務端回話,再次一般初始化接受數據的對象
//NSLog(@"返回數據類型%@",[response ]);
//NSLog(@"返回數據編碼%@",[response text]);
NSMutableData* data = [[NSMutableDataalloc]init];
self.data = data;
[data release];
}
//不斷的獲取數據
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//接受返回數據,這個方法可能會被調用多次,因此將多次返回數據加起來
NSInteger datalength = [datalength];
NSLog(@"返回數據量:%d",datalength);
[self.dataappendData:data];
}
//獲取文件地址
-(NSString *)dataFilePath:(NSString*)fileName
{
NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *document=[pathsobjectAtIndex:0];
return[documentstringByAppendingPathComponent:fileName];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//? ? //連接結束
NSLog(@"%d",[self.datalength]);
self.label.text =@"請求結束";
//可以下載圖片
//[self.data writeToFile:[self dataFilePath:@"image.jpg"] atomically:YES];
NSString* mystr = [[NSStringalloc]initWithData:self.dataencoding:NSUTF8StringEncoding];
[mystr writeToFile:[selfdataFilePath:@"百度圖片—全球最大中文圖片庫.html"] atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"最后的結果%@",mystr);
[mystr release];
//? ? NSDictionary *weather = [NSJSONSerialization JSONObjectWithData:self.data options:NSJSONReadingMutableContainers? error:nil];
//? ? NSLog(@"%@",weather);
//? ? [weather writeToFile:[self dataFilePath:@"weather.plist"] atomically:YES];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
self.label.text =@"連接失敗";
}
- (IBAction)searchClick:(id)sender {
}
@end
總結
以上是生活随笔為你收集整理的ios 获取html中的json数据,[IOS]UIWebView实现保存页面和读取服务器端json数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Python爬虫】使用urllib.r
- 下一篇: Fiddler抓包 | 竟然有这些骚操作