IOS 网络编程 代码
// 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