很久没有更新博客了,所以分享一个。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 |
@protocol
HttpListenerDelegate; @interface
BaseHttp : NSObject { NSURLConnection * _connect; NSMutableData * _receiveData; NSString
*_httpUrl; NSString
*_identify; __weak id <HttpListenerDelegate> _delegate; } @property
( nonatomic , weak) id <HttpListenerDelegate> delegate; @property
( nonatomic , M_STRONG) NSURLConnection
*connect; @property
( nonatomic , M_STRONG) NSMutableData
*receiveData; @property
( nonatomic , M_STRONG) NSString
*httpUrl; //设置当前服务的唯一标示,默认为当前的URL @property
( nonatomic , M_STRONG) NSString
*identify; - ( id )initWithHttpUrl:( NSString
*)url; //开始调用远程服务 - ( void )execute; - ( void )execute:( id )param; //接收到服务器回应的时候调用此方法 - ( void )connection:( NSURLConnection
*)connection didReceiveResponse:( NSURLResponse
*)response; //接收到服务器传输数据的时候调用,此方法根据数据大小执行若干次 - ( void )connection:( NSURLConnection
*)connection didReceiveData:( NSData
*)data; //数据传完之后调用此方法 - ( void )connectionDidFinishLoading:( NSURLConnection
*)connection; //网络请求过程中,出现任何错误(断网,连接超时等)会进入此方法 - ( void )connection:( NSURLConnection
*)connection didFailWithError:( NSError
*)error; @end @protocol
HttpListenerDelegate < NSObject > @optional //接收到服务器回应的时候调用此方法 - ( void )didReceiveResponse:( NSURLResponse
*)response identify:( NSString
*)identify; - ( void )didReceiveData:( NSData
*)data identify:( NSString
*)identify; //后台加载数据完成 - ( void )didFinishLoading:( NSMutableData *)receiveData identify:( NSString
*)identify; //网络请求异常 - ( void )didFailWithError:( NSError
*)error identify:( NSString
*)identify; @end |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 |
// // BaseHttp.m // myb-ios // // Created by warrior gao on 13-6-7. // Copyright (c) 2013年 51myb. All rights reserved. // #import "BaseHttp.h" @implementation
BaseHttp @synthesize
connect = _connect, receiveData = _receiveData, delegate = _delegate, httpUrl = _httpUrl, identify = _identify; - ( id )initWithHttpUrl:( NSString
*)url { self
= [ self init]; _httpUrl = [ NSString
stringWithFormat: @ "%@%@" ,SERVER_URL, url]; _identify = url; return
self ; } -( void )setHttpUrl:( NSString
*)httpUrl { _httpUrl = httpUrl; if (!(_identify)) _identify = httpUrl; } //开始调用远程服务 - ( void )execute { [ self
execute:@ "" ]; } - ( void )execute:( id )param { if (DEBUG) { NSLog (@ "开始请求:%@" , _httpUrl); } //第一步,创建URL NSURL
*url = [ NSURL
URLWithString:_httpUrl]; //第二步,创建请求 NSMutableURLRequest
*request = [[ NSMutableURLRequest
alloc]initWithURL:url cachePolicy: NSURLRequestUseProtocolCachePolicy
timeoutInterval:DEFAULT_HTTP_TIMEOUT]; [request setHTTPMethod:@ "POST" ]; //设置请求方式为POST,默认为GET [request addValue:@ "application/json"
forHTTPHeaderField:@ "Content-Type" ]; [request addValue:HTTP_HEADER_VALUE forHTTPHeaderField:HTTP_HEADER_KEY]; NSData
*bodyData = nil ; if ([param isKindOfClass:[ NSString
class ]]){ bodyData = [param dataUsingEncoding: NSUTF8StringEncoding ]; } else
if ([param isKindOfClass:[ NSData
class ]]){ bodyData = param; } else
if ([param isKindOfClass:[ NSNumber
class ]]) { bodyData = [[param stringValue] dataUsingEncoding: NSUTF8StringEncoding ]; } [request setHTTPBody:bodyData]; //第三步,连接服务器 _connect = [[ NSURLConnection
alloc] initWithRequest:request delegate: self ]; if (_connect){ _receiveData = [ NSMutableData
data]; } } //接收到服务器回应的时候调用此方法 - ( void )connection:( NSURLConnection
*)connection didReceiveResponse:( NSURLResponse
*)response { [_receiveData setLength:0]; if ([_delegate respondsToSelector: @selector (didReceiveResponse:identify:)]) [_delegate didReceiveResponse:response identify:_identify]; } //接收到服务器传输数据的时候调用,此方法根据数据大小执行若干次 -( void )connection:( NSURLConnection
*)connection didReceiveData:( NSData
*)data { [_receiveData appendData:data]; if ([_delegate respondsToSelector: @selector (didReceiveData:identify:)]) [_delegate didReceiveData:data identify:_identify]; } //数据传完之后调用此方法 -( void )connectionDidFinishLoading:( NSURLConnection
*)connection { if (DEBUG){ NSLog (@ "请求后台数据完成---:%@" ,_identify); } if (DEBUG){ NSString
*receiveStr = [[ NSString
alloc] initWithData:_receiveData encoding: NSUTF8StringEncoding ]; NSLog (@ "%@" ,receiveStr); } if ([_delegate respondsToSelector: @selector (didFinishLoading:identify:)]) [_delegate didFinishLoading:_receiveData identify:_identify]; } //网络请求过程中,出现任何错误(断网,连接超时等)会进入此方法 - ( void )connection:( NSURLConnection
*)connection didFailWithError:( NSError
*)error { if (DEBUG){ NSLog (@ "%@" ,[error localizedDescription]); } if ([_delegate respondsToSelector: @selector (didFailWithError:identify:)]) [_delegate didFailWithError:error identify:_identify]; else
{ [AlertViewHelper alertMessage:HTTP_CONNECT_ERROR]; } } @end |
分享一个自己用的Objective-C的Http接连类,布布扣,bubuko.com
原文:http://www.cnblogs.com/warrior/p/3598561.html