日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ios 获取html中的json数据,[IOS]UIWebView实现保存页面和读取服务器端json数据

發布時間:2025/3/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。