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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值