iOS简单封装网络请求,使用block回掉函数方式

//
002 //  RequestHttpClass.h
003 //  封装简单的异步请求
004  
005 #import <Foundation/Foundation.h>
006  
007 // 相当于定义一个函数指针
008 typedef void(^FinishLoadBlock)(NSData *);
009  
010 @interface RequestHttpClass : NSMutableURLRequest <NSURLConnectionDataDelegate>
011  
012 // 存储异步请求的数据
013 @property (nonatomic, retain) NSMutableData *data;
014 @property (nonatomic, retain) NSURLConnection *connection;
015 @property (nonatomic, copy)FinishLoadBlock block;
016  
017 - (void)startAsync;
018 - (void)cancel;
019 @end
020  
021  
022 //
023 //  RequestHttpClass.m
024 //  RequestClass
025 //
026 //  Copyright (c) 2013年 TestApp. All rights reserved.
027 //
028  
029 #import "RequestHttpClass.h"
030  
031 @implementation RequestHttpClass
032  
033 -(void)startAsync {
034     self.data = [[NSMutableData alloc] init];
035      
036      // 发起异步请求
037     self.connection = [NSURLConnection connectionWithRequest:self delegate:self];
038      
039 }
040  
041 - (void)cancel {
042     [self.connection cancel];
043 }
044  
045 // 异步请求每次返回的数据
046 - (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
047     [self.data appendData:data];
048 }
049  
050 // 异步请求结束数据返回
051 - (void) connectionDidFinishLoading:(NSURLConnection *)connection {
052     self.block(_data);
053 }
054  
055 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
056     NSLog(@"请求网络出错:%@", error);
057 }
058  
059 -(void) dealloc {
060     [_data release];
061     [super dealloc];
062 }
063  
064 //
065 //  RequestData.h
066 //  RequestClass
067 //
068 //  Copyright (c) 2013年 TestApp. All rights reserved.
069 //
070  
071 #import <Foundation/Foundation.h>
072  
073 typedef void(^Completion)(id);
074  
075 @interface RequestData : NSObject
076  
077 // 访问天气预报接口
078 + (void)getWeatherData:(NSDictionary *)params block: (Completion) block;
079  
080 @end
081  
082  
083 //
084 //  RequestData.m
085 //  RequestClass
086 //
087  
088 //  Copyright (c) 2013年 TestApp. All rights reserved.
089 //
090  
091 #import "RequestData.h"
092 #import "RequestHttpClass.h"
093  
094 #define BASE_URL @"http://www.weather.com.cn/data/sk/"
095  
096 @implementation RequestData
097  
098 + (void)getWeatherData:(NSDictionary *)params block:(Completion )block {
099      
100     NSString *cityCode = [params objectForKey:@"code"];
101     NSString *urlstring = [BASE_URL stringByAppendingFormat:@"%@.html",cityCode];
102      
103     RequestHttpClass *request = [RequestHttpClass requestWithURL:[NSURL URLWithString:urlstring]];
104     [request setHTTPMethod:@"GET"];
105     [request setTimeoutInterval:60];
106      
107     request.block = ^(NSData *data) {
108         id ret = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
109         block(ret);
110     };
111      
112     [request startAsync];
113 }
114 @end
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值