// |
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 |