很久沒有更新博客了,所以分享一個。
@protocol HttpListenerDelegate;
@interface BaseHttp : NSObject
{
}
@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
// // BaseHttp.m // myb-ios // // Created by warrior gao on 13-6-7. // Copyright (c) 2013年 51myb. All rights reserved. // #import "BaseHttp.h" @implementation BaseHttp
- (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
