Created
September 26, 2014 03:06
-
-
Save qingjoin/25dad6d1b5413d960639 to your computer and use it in GitHub Desktop.
iOS NSURLConnection 封装
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// QyHttpRequest.h | |
// QySdkDemo | |
// | |
// Created by qingyun on 6/18/14. | |
// Copyright (c) 2014 qingyun. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import <UIKit/UIKit.h> | |
@protocol QyHttpRequestDelegate ; | |
@interface QyHttpRequest : NSObject<NSURLConnectionDelegate> | |
{ | |
NSMutableURLRequest *_request; | |
NSString *_dataLength; | |
NSDictionary *_responesDic; | |
NSURLConnection *_connection; | |
} | |
@property(nonatomic,assign)id<QyHttpRequestDelegate> delegate; | |
@property(nonatomic,assign)CGFloat timeOut; //超时 | |
+ (id)requestWithURL:(NSURL *)newURL; //url | |
-(void)setHTTPMethod:(NSString*)method; | |
-(void)addRequestHeader:(NSString *)header value:(NSString *)value; //头里添加参数 | |
-(void)setValue:(id)value forKey:(NSString*)key; //以Key value的方式传值 | |
-(void)setHTTPBody:(NSData*)data; //传参 | |
-(void)setHttpStart; | |
@end | |
//delegate 代理 | |
@protocol QyHttpRequestDelegate <NSObject> | |
-(void)didRequestFinished:(NSDictionary*)request; //请求结束 | |
-(void)didRequestFailed:(NSError*)error; //请求出错 | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// QyHttpRequest.m | |
// QySdkDemo | |
// | |
// Created by qingyun on 6/18/14. | |
// Copyright (c) 2014 qingyun. All rights reserved. | |
// | |
#import "QyHttpRequest.h" | |
static NSMutableURLRequest *urlCYOURequest = nil; | |
static QyHttpRequest *glCYOUHttp = nil; | |
//static NSDictionary *responesDic = nil; | |
@interface QyHttpRequest () | |
{ | |
} | |
@property(nonatomic,strong)NSDictionary *responesDic; | |
@end | |
@implementation QyHttpRequest | |
@synthesize delegate; | |
@synthesize timeOut; | |
+(id)requestWithURL:(NSURL *)newURL | |
{ | |
@synchronized(self) | |
{ | |
if(glCYOUHttp == nil) | |
{ | |
glCYOUHttp = [[self alloc]init]; | |
} | |
[urlCYOURequest setURL:newURL]; | |
} | |
NSLog(@"newURL:%@",newURL); | |
return glCYOUHttp; | |
} | |
-(id)init | |
{ | |
self = [super init]; | |
if(self) | |
{ | |
urlCYOURequest = [[NSMutableURLRequest alloc] init]; | |
self.responesDic = [[NSDictionary alloc]init]; | |
} | |
return self; | |
} | |
-(void)setHTTPMethod:(NSString *)method | |
{ | |
[urlCYOURequest setHTTPMethod:method]; | |
} | |
- (void)addRequestHeader:(NSString *)header value:(NSString *)value | |
{ | |
if([header length] && [value length]) | |
{ | |
[urlCYOURequest setValue:value forHTTPHeaderField:header]; | |
} | |
else | |
{ | |
NSLog(@"addRequestHeader-Value null"); | |
} | |
} | |
-(void)setValue:(id)value forKey:(NSString*)key | |
{ | |
if(value && [key length]) | |
{ | |
[urlCYOURequest setValue:value forKey:key]; | |
} | |
} | |
#pragma mark post 值 | |
-(void)setHTTPBody:(NSData*)data; | |
{ | |
[urlCYOURequest setHTTPBody:data]; | |
_dataLength = [NSString stringWithFormat:@"%d", [data length]]; | |
[self setDefault]; //一些默认设置 | |
} | |
-(void)setHTTPBodyNonSyschronous:(NSData*)data; | |
{ | |
_dataLength = [NSString stringWithFormat:@"%d", [data length]]; | |
[urlCYOURequest setHTTPBody:data]; | |
[self setDefault]; //一些默认设置 | |
[urlCYOURequest setTimeoutInterval:timeOut]; // 设置超时 | |
//__unsafe_unretained __block __weak | |
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlCYOURequest delegate:self]; | |
if (connection == nil) | |
{ | |
NSLog(@"errors"); | |
// 创建失败 | |
return; | |
} | |
} | |
#pragma mark get country code //异步 | |
-(void)setHttpStart | |
{ | |
[self setDefault]; //一些默认设置 | |
[urlCYOURequest setTimeoutInterval:timeOut]; // 设置超时 | |
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlCYOURequest delegate:self]; | |
if (connection == nil) | |
{ | |
NSLog(@"errors"); | |
// 创建失败 | |
} | |
} | |
// 收到回应 | |
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response | |
{ | |
NSLog(@"receive the response"); | |
} | |
// 接收数据 | |
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data | |
{ | |
NSError *error = nil; | |
NSMutableData *saveData = [[NSMutableData alloc]init]; | |
[saveData appendData:data]; | |
//NSLog(@"datalength:%@",dataLength); | |
//NSLog(@"requestDDDD:%@",data); | |
NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; | |
NSLog(@"requestData:%@",aStr); | |
if(aStr) | |
{ | |
self.responesDic = [NSDictionary dictionaryWithObject:aStr forKey:@"Pc_JsonData"]; | |
return; | |
} | |
if(saveData) | |
{ | |
self.responesDic = [NSJSONSerialization JSONObjectWithData:saveData options:NSJSONReadingMutableLeaves error:&error]; | |
} | |
} | |
// 数据接收完毕 | |
- (void)connectionDidFinishLoading:(NSURLConnection *)connection | |
{ | |
[delegate didRequestFinished:self.responesDic]; | |
} | |
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error | |
{ | |
[self.delegate didRequestFailed:error]; | |
NSLog(@"Connection failed: %@", error); | |
} | |
-(void)setDefault | |
{ | |
[urlCYOURequest setValue:@"aCYOUlication/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; | |
[urlCYOURequest setValue:_dataLength forHTTPHeaderField:@"Content-Length"]; | |
if(timeOut==0) | |
{ | |
timeOut = 30; //默认设置20超时 | |
} | |
} | |
#pragma mark get start 异步 | |
-(void)setHttpGetStart | |
{ | |
[urlCYOURequest setHTTPMethod:@"GET"]; | |
[self setDefault]; //一些默认设置 | |
[urlCYOURequest setTimeoutInterval:timeOut]; // 设置超时 | |
//__unsafe_unretained __block __weak | |
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlCYOURequest delegate:self]; | |
if (connection == nil) | |
{ | |
NSLog(@"errors"); | |
// 创建失败 | |
return; | |
} | |
return; | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
NSError *error = nil; | |
NSHTTPURLResponse* urlResponse = nil; | |
NSData *responseData = [NSURLConnection sendSynchronousRequest:urlCYOURequest returningResponse:&urlResponse error:&error]; | |
dispatch_sync(dispatch_get_main_queue(), ^{ //防卡死 | |
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; | |
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) | |
{ | |
NSLog(@"Outdata:%@",result); | |
NSError *errors = nil; | |
self.responesDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&errors]; | |
NSLog(@"self.responesDic:%@",self.responesDic); | |
dispatch_sync(dispatch_get_main_queue(), ^{ //防卡死 | |
[self.delegate didRequestFinished:self.responesDic]; | |
}); | |
} | |
else | |
{ | |
[self.delegate didRequestFailed:error]; | |
} | |
}); | |
}); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment