urlurl
2015-06-24 20:07
260 查看
伤心总是难免的,何苦一往情深,总之爱情难舍难分,何必在意那一点点温存,早知道伤心总是难免的,何苦一往情深,,有的人你永远不会懂
#import "ViewController.h"
#pragma mark - 重定义网址
#define BASE_URL_1 @"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_URL_2 @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?"
#define BASE_URL_2_PARAM @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"
#pragma mark - 添加异步链接的代理--异步需代理实现
@interface ViewController ()<NSURLConnectionDelegate,NSURLConnectionDataDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property(nonatomic, strong) NSMutableData *allData; //声明一个属性,异步代理方法执行
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - get同步
- (IBAction)getSyncAction:(UIButton *)sender {
//1.网络地址
NSURL *url = [NSURL URLWithString:BASE_URL_1];
//2.通过网址创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//2.1.设置请求方式
[request setHTTPMethod:@"GET"];
//3创建连接对象,发送请求,获取数据
//3.1获取返回信息的对象
NSURLResponse *response = [NSURLResponse new];
//3.2保存错误信息的对象
NSError *error = nil;
//3.3发送请求
/**
* request:发送的请求
response:接受结果的信息
error:保存发生错误时候的错误信息
*/
NSData *resdata = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//接收到的结果都是二进制
//4.解析---把数据解析为dict
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:resdata options:NSJSONReadingAllowFragments error:nil];
//下面是练习
// NSURL *url = [NSURL URLWithString:BASE_URL_1];
// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// [request setHTTPMethod:@"GET"];
// NSURLResponse *response = [NSURLResponse new];
//
// NSData *resdata = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
// NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:resdata options:NSJSONReadingAllowFragments error:nil];
}
#pragma mark - get异步 --- 需要5个方法
- (IBAction)getAsyncAction:(UIButton *)sender {
//1.网络地址
NSURL *url = [NSURL URLWithString:BASE_URL_1];
//2.通过网址创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//2.1.设置请求方式
[request setHTTPMethod:@"GET"];
//每个步骤 都要用代理方法走
//3.使用异步链接,发送请求
[NSURLConnection connectionWithRequest:request delegate:self];
}
#pragma mark - post同步method
- (IBAction)postSyncAction:(UIButton *)sender {
//1.准备网址
NSURL *url = [NSURL URLWithString:BASE_URL_2];
//2.请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"]; //设置请求方式
//将字符串转化为data类型的数据
NSData *paramData = [BASE_URL_2_PARAM dataUsingEncoding:NSUTF8StringEncoding];
//设置请求参数(data类型)
[request setHTTPBody:paramData];
//3.创建链接对象,发送请求,获取连接数据
NSData *resData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"%@", resData);
//4.解析
}
#pragma mark - POST异步
- (IBAction)postAyncAction:(UIButton *)sender {
//1.准备网址
NSURL *url = [NSURL URLWithString:BASE_URL_2];
//2.请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"]; //设置请求方式
//将字符串转化为data类型的数据
NSData *paramData = [BASE_URL_2_PARAM dataUsingEncoding:NSUTF8StringEncoding];
//设置请求参数(data类型)
[request setHTTPBody:paramData];
//3.使用连接,发送请求,获取数据
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%@", data);
//解析
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
}];
}
- (IBAction)cleatAction:(UIButton *)sender {
}
#pragma mark - 异步代里方法 --
#pragma mark - 已经接受响应 - 执行1次
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//1
#import "ViewController.h"
#pragma mark - 重定义网址
#define BASE_URL_1 @"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_URL_2 @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?"
#define BASE_URL_2_PARAM @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"
#pragma mark - 添加异步链接的代理--异步需代理实现
@interface ViewController ()<NSURLConnectionDelegate,NSURLConnectionDataDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property(nonatomic, strong) NSMutableData *allData; //声明一个属性,异步代理方法执行
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - get同步
- (IBAction)getSyncAction:(UIButton *)sender {
//1.网络地址
NSURL *url = [NSURL URLWithString:BASE_URL_1];
//2.通过网址创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//2.1.设置请求方式
[request setHTTPMethod:@"GET"];
//3创建连接对象,发送请求,获取数据
//3.1获取返回信息的对象
NSURLResponse *response = [NSURLResponse new];
//3.2保存错误信息的对象
NSError *error = nil;
//3.3发送请求
/**
* request:发送的请求
response:接受结果的信息
error:保存发生错误时候的错误信息
*/
NSData *resdata = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//接收到的结果都是二进制
//4.解析---把数据解析为dict
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:resdata options:NSJSONReadingAllowFragments error:nil];
//下面是练习
// NSURL *url = [NSURL URLWithString:BASE_URL_1];
// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// [request setHTTPMethod:@"GET"];
// NSURLResponse *response = [NSURLResponse new];
//
// NSData *resdata = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
// NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:resdata options:NSJSONReadingAllowFragments error:nil];
}
#pragma mark - get异步 --- 需要5个方法
- (IBAction)getAsyncAction:(UIButton *)sender {
//1.网络地址
NSURL *url = [NSURL URLWithString:BASE_URL_1];
//2.通过网址创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//2.1.设置请求方式
[request setHTTPMethod:@"GET"];
//每个步骤 都要用代理方法走
//3.使用异步链接,发送请求
[NSURLConnection connectionWithRequest:request delegate:self];
}
#pragma mark - post同步method
- (IBAction)postSyncAction:(UIButton *)sender {
//1.准备网址
NSURL *url = [NSURL URLWithString:BASE_URL_2];
//2.请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"]; //设置请求方式
//将字符串转化为data类型的数据
NSData *paramData = [BASE_URL_2_PARAM dataUsingEncoding:NSUTF8StringEncoding];
//设置请求参数(data类型)
[request setHTTPBody:paramData];
//3.创建链接对象,发送请求,获取连接数据
NSData *resData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"%@", resData);
//4.解析
}
#pragma mark - POST异步
- (IBAction)postAyncAction:(UIButton *)sender {
//1.准备网址
NSURL *url = [NSURL URLWithString:BASE_URL_2];
//2.请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"]; //设置请求方式
//将字符串转化为data类型的数据
NSData *paramData = [BASE_URL_2_PARAM dataUsingEncoding:NSUTF8StringEncoding];
//设置请求参数(data类型)
[request setHTTPBody:paramData];
//3.使用连接,发送请求,获取数据
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%@", data);
//解析
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
}];
}
- (IBAction)cleatAction:(UIButton *)sender {
}
#pragma mark - 异步代里方法 --
#pragma mark - 已经接受响应 - 执行1次
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//1
相关文章推荐
- 我的第一个博客
- 07-排序2. Insert or Merge (25)
- 第1章 游戏之乐——让CPU占用率曲线听你指挥
- LeetCode:Same Tree
- android---xml---include用法(导入另一个XML文件到本布局中)
- 由普利姆算法想到一个游戏之后
- nsurl
- Windows live Writer博客园,51CTO,网易博客,新浪博客配置方法
- xml解析与保存
- Tomcat 系统架构与设计模式,第 1 部分: 工作原理图 1.Tomcat 的总体结构
- 大型商贸系统(进货管理)技术解析(二)自营进仓单录入
- MFC对话框应用程序添加自定义消息
- Minimum Depth of Binary Tree
- DHCP服务器搭建与管理
- Linux下select应用
- Linux系统编程——vfork() 函数详解
- Android OpenGL ES(四)----调整屏幕的宽高比
- VFL与Masonry
- Android基础知识_Activity SingleTask模式示例
- Excel Sheet Column Title