先声明
#import "CWViewController.h"
//get请求的url
#define BASE_URL @"http://project.lanou3g.com/teacher/yihuiyun/phpJSON.php"
//post请求的url
#define BASE_URL_2 @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"
//post请求包体
#define BASE_URL_2_PARAM @"date=20131129&startRecord=5&len=5&udid=1234567890&terminalType=Iphone&cid=215"
@interface CWViewController ()<NSURLConnectionDataDelegate>
@property (strong, nonatomic) IBOutlet UITextView *textView;
//接收数据
@property(strong,nonatomic)NSMutableData *reData;
@end
1 : Get同步
- (IBAction)getT:(id)sender {
NSLog(@"get同步");
//1.准备一个URL
NSString *urlString=BASE_URL;
NSURL *url=[NSURL URLWithString:urlString];
//2.创建请求对象,不可变的默认get
NSMutableURLRequest *requst=[NSMutableURLRequest requestWithURL:url];
//请求方式
[requst setHTTPMethod: @"GET"];
//3.创建响应对象
NSURLResponse *response=nil;
//如果出错了,也能接收
NSError *error=nil;
//4.建立连接
NSData *data= [NSURLConnection sendSynchronousRequest:requst returningResponse:&response error:&error];
// NSLog(@"%@",data);
NSArray *array=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSString *s=[NSString stringWithFormat:@"%@",array];
//返回的类型
NSLog(@"%@",response.MIMEType);;
//返回内容的长度
NSLog(@"%lld",response.expectedContentLength);
//如果有错误,打印错误简介
NSLog(@"%@",error.localizedDescription);
self.textView.text=s;
}
2: Post同步
- (IBAction)postT:(id)sender {
NSLog(@"post同步");
//第一步:准备url
NSURL *url=[NSURL URLWithString:BASE_URL_2];
//第二部:创建请求对象
NSMutableURLRequest *requst=[NSMutableURLRequest requestWithURL:url];
//设置请求方式
[requst setHTTPMethod:@"POST"];
//将字符串转成NSData
NSString *bodyStr=BASE_URL_2_PARAM;
NSData *bodyData=[bodyStr dataUsingEncoding:NSUTF8StringEncoding];
//给请求的设置body
[requst setHTTPBody:bodyData];
//3.创建连接
NSData *data=[NSURLConnection sendSynchronousRequest:requst returningResponse:nil error:nil];
NSLog(@"%@",data);
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
self.textView.text=[NSString stringWithFormat:@"%@",dic];
}
3: Get异步 (代理)
//get异步 (代理)
- (IBAction)getYD:(id)sender {
NSLog(@"get异步 代理");
//1.准备URl
NSURL *url=[NSURL URLWithString:BASE_URL];
//创建请求对象
NSURLRequest *request=[NSURLRequest requestWithURL:url];
//3.创建连接(同时设置代理)
NSURLConnection *conn=[NSURLConnection connectionWithRequest:request delegate:self];
//4.启动
[conn start];
NSLog(@"+++++++++++++++++++++++++++++++++++++++++++");
// UIActionSheet *as=[[UIActionSheet alloc] initWithTitle:@"警告" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"呵呵", nil];
// [as showInView:self.view];
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//接收到响应
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
self.reData=[NSMutableData data];
}
//接收到数据(不仅仅走一遍)
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
//数据拼接
[self.reData appendData:data];
}
//数据接收完成
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"%@",self.reData);
NSLog(@"-----------------------------------------------");
//get异步代理
/*
NSArray *array=[NSJSONSerialization JSONObjectWithData:self.reData options:NSJSONReadingMutableContainers error:nil];
NSString *s=[NSString stringWithFormat:@"%@",array];
self.textView.text=s;
*/
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:self.reData options:NSJSONReadingMutableContainers error:nil];
self.textView.text=[NSString stringWithFormat:@"%@",dic];
}
//出错
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
}
4 : Post异步(代理)
- (IBAction)postYD:(id)sender {
NSLog(@"post异步代理");
//1;准备url
NSURL *url=[NSURL URLWithString:BASE_URL_2];
//第二部:创建请求对象
NSMutableURLRequest *requst=[NSMutableURLRequest requestWithURL:url];
//设置请求方式
[requst setHTTPMethod:@"POST"];
//将字符串转成NSData
NSString *bodyStr=BASE_URL_2_PARAM;
NSData *bodyData=[bodyStr dataUsingEncoding:NSUTF8StringEncoding];
//给请求的设置body
[requst setHTTPBody:bodyData];
//创建连接并设置代理
NSURLConnection *conn=[NSURLConnection connectionWithRequest:requst delegate:self];
[conn start];
}
//接收到响应
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
self.reData=[NSMutableData data];
}
//接收到数据(不仅仅走一遍)
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
//数据拼接
[self.reData appendData:data];
}
//数据接收完成
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"%@",self.reData);
NSLog(@"-----------------------------------------------");
//get异步代理
/*
NSArray *array=[NSJSONSerialization JSONObjectWithData:self.reData options:NSJSONReadingMutableContainers error:nil];
NSString *s=[NSString stringWithFormat:@"%@",array];
self.textView.text=s;
*/
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:self.reData options:NSJSONReadingMutableContainers error:nil];
self.textView.text=[NSString stringWithFormat:@"%@",dic];
}
//出错
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
}
5 : Get异步(block)
- (IBAction)getYBlock:(id)sender {
NSLog(@"get异步block");
//1:准备url
NSURL *url=[NSURL URLWithString:BASE_URL];
//2:创建请求对象
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
//设置request
[request setHTTPMethod:@"GET"];
//3.创建响应对象
NSURLResponse *response=nil;
//如果出错了,也能接收
NSError *error=nil;
//4.建立连接
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSArray *array=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSString *s=[NSString stringWithFormat:@"%@",array];
self.textView.text=s;
}];
}
6: Post异步(block)
- (IBAction)postYB:(id)sender {
NSLog(@"post异步(block)");
//1:准备url
NSURL *url=[NSURL URLWithString:BASE_URL_2];
//第二部:创建请求对象
NSMutableURLRequest *requst=[NSMutableURLRequest requestWithURL:url];
//设置request
[requst setHTTPMethod:@"POST"];
NSData *bodyData=[BASE_URL_2_PARAM dataUsingEncoding:NSUTF8StringEncoding];
[requst setHTTPBody:bodyData];
//创建连接对象
[NSURLConnection sendAsynchronousRequest:requst queue:[NSOperationQueue mainQueue]completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%@",data);
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
self.textView.text=[NSString stringWithFormat:@"%@",dic];
}];
}