//————————————————————————————————————————————————————————————————————————————
// 0.文件很小的時候可以不使用請求的方法(壞處1、在主線程中,訪問服務器的時候會卡死 2、文件太大的時候,一次性傳輸,服務器受不了)
// NSURL * url = [NSURL URLWithString:@"http://192.168.2.162/logo.php?userName=jereh&pwd=123"];
// NSData * data = [NSData dataWithContentsOfURL:url];
// NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSLog(@"%@", str);
// get請求(代理方式)
//// NSURL * url = [NSURL URLWithString:@"http://192.168.2.162/logo.php?userName=jereh&pwd=123"];
// NSURL * url = [NSURL URLWithString:@"http://192.168.2.162/test2.rar"];//請求大的數據
//
// //通過URL建立請求對象
// NSURLRequest * request = [NSURLRequest requestWithURL:url];
//
// //創建NSURLConnection 對象用來連接服務器並且發送請求
// NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
// [conn start];//新版本可以不用寫
//————————————————————————————————————————————————————————————————————————————
//請求主要使用的四個代理方法(異步的方法)
//接受到相應(只調用一次,請求成功,發送數據前調用)
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
resultData = [NSMutableData data];//數據接收的對象一般在這裏初始化
}
//接受到數據(發送數據的時候調用,大文件會自動分塊傳輸,這個方法調用多次)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[resultData appendData:data];//服務器每一次發送多少數據
NSLog(@"%li", resultData.length);
}
//結束下載(數據傳輸完畢)
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:resultData options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@", dic);
}
//請求失敗(鏈接不上服務器,網址錯誤會調用。密碼不對等不會調用)
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%@", error);
}
@end