#import "ViewController.h"
@interfaceViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//請求網絡頁面
// 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:請求位址
//儲存頁面
//通路伺服器擷取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:request delegate: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 {
2013-08-27 16:09:18.821 WebViewDemo[673:c07] 建立連結成功
2013-08-27 16:09:19.161 WebViewDemo[673:c07] 傳回資料量:153
2013-08-27 16:09:19.161 WebViewDemo[673:c07] 153
2013-08-27 16:09:19.193 WebViewDemo[673:c07] 最後的結果{"weatherinfo":{"city":"上海","cityid":"101020100","temp1":"32℃","temp2":"27℃","weather":"多雲","img1":"d1.gif","img2":"n1.gif","ptime":"11:00"}}
==================== 迂者 丁小未 CSDN部落格專欄=================
Unity QQ群:858550 cocos2dx QQ群:280818155
====================== 互相學習,共同進步 ===================
<a target="_blank"></a>