標籤:
// ViewController.m
// 16_網路編程
// Created by lanou3g on 14-12-19.
// Copyright (c) 2014年 mxt. All rights reserved.
#import "ViewController.h"
#define BASE_URL @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"
#define BASE_POST_URL @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?"
#define POST @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"
@interface ViewController ()<NSURLConnectionDataDelegate>
@property(nonatomic,retain)NSMutableData *mutaData;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
/*
不同點:
1、給伺服器傳輸資料的?方式:
GET :通過網址字串。
POST:通過data
2、傳輸資料的大小:
GET :網址字串最多255位元組。
POST:使?用NSData,容量超過1G
3、安全性:
GET:所有傳輸給伺服器的資料,顯示在網址?裡,類似於密碼的明?輸入,直接可見。
POST:資料被轉成NSData(位元據),類似於密碼的密?輸入,無法直接讀取
*/
//GET 同步
- (IBAction)getSyncButton:(id)sender {
//1, 建立URL
NSURL *url = [NSURL URLWithString:BASE_URL];
//2, 建立NSURLRequest 請求對象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//2, 1
[request setHTTPMethod:@"GET"];
//3,返回結果對象:傳回值資訊
NSURLResponse *response = nil;
NSError *error = nil;
//4,建立連結化物件 NSURLConnection
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// NSLog(@"傳回型別 %@ ",response);//列印傳回值資訊
// NSLog(@"--------------------%@",data);
//6 解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//NSLog(@"+++++++++%@",dic);
// NSLog(@"%@",[dic valueForKey:@"news"]);
}
//post 同步
- (IBAction)postSyncButton:(id)sender {
//1,建立URL
NSURL *url = [NSURL URLWithString:BASE_POST_URL];
//1,1 佈建要求體中的參數,進行編碼
NSString *post = [NSString stringWithFormat:POST];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
//2,建立NSURLRequest 請求對象(可變 是因為要設定參數)
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//2,1
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
NSError *error = nil;
//3,建立連結化物件 NSURLConnection (同步 不設定代理)
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
// NSLog(@"--------------------%@",data);
//6 解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"+++++++++%@",dic);
// NSLog(@"%@",[dic valueForKey:@"news"]);
}
/*
非同步聯結有兩種實現?方式:
1, 設定代理,接收資料
2, 實現block(多線程)
*/
//GET非同步
- (IBAction)getAsyncButton:(id)sender {
// GET非同步 + block
//1, 建立URL
NSURL *url = [NSURL URLWithString:BASE_URL];
//2, 建立NSURLRequest 請求對象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//2,1
[request setHTTPMethod:@"GET"];
//建立操作隊列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//3,建立連結化物件 NSURLConnection (在block 內部完成解析)
__block ViewController *weakSelf = self;
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//3,1 解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//NSLog(@"+++++++++%@",dic);
//永遠不要在子線程更新UI
dispatch_async(dispatch_get_main_queue(), ^{
//回到主線程 更新UI
weakSelf.textView.text = [NSString stringWithFormat:@"%@",dic];
});
}];
NSLog(@">>>>>>>>>>>>>>><<<>>%lu",_textView.retainCount);
[queue release];
/*
//1, 建立URL
NSURL *url = [NSURL URLWithString:BASE_URL];
//2, 建立NSURLRequest 請求對象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//2,1
[request setHTTPMethod:@"GET"];
//3, 建立連結化物件 發送請求 設定代理
[NSURLConnection connectionWithRequest:request delegate:self];
*/
}
#pragma mark -NSURLConnectionDataDelegate
//開始接受
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)respons
{
NSLog(@"開始接受");
self.mutaData = [NSMutableData data];
NSLog(@"%lu",_mutaData.retainCount);
}
//接受資料
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"---------接受資料");
[self.mutaData appendData:data];
}
//接受完畢
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"++接受完畢");
//解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:_mutaData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",dic);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"錯誤");
}
//POST 非同步
- (IBAction)postAsyncButton:(id)sender {
//1,建立URL
NSURL *url = [NSURL URLWithString:BASE_POST_URL];
//1,1 佈建要求體中的參數,進行編碼
NSString *post = [NSString stringWithFormat:POST];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
//2,建立NSURLRequest 請求對象(可變 是因為要設定參數)
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//2,1
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
//建立操作隊列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//3,建立連結化物件 NSURLConnection (在block 內部完成解析)
__block ViewController *weakSelf = self;
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//3,1 解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//NSLog(@"+++++++++%@",dic);
//永遠不要在子線程更新UI
dispatch_async(dispatch_get_main_queue(), ^{
//回到主線程 更新UI
weakSelf.textView.text = [NSString stringWithFormat:@"%@",dic];
});
}];
NSLog(@">>>>>>>>>>>>>>><<<>>%lu",_textView.retainCount);
[queue release];
}
- (void)dealloc
{
[_mutaData release];
[_textView release];
[super dealloc];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
IOS 網路編程 代碼