网络解析

先声明

#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];

        
    }];

    
    
    
    
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值