一般网络请求
#pragma mark - 加载数据
- (void)loadData {
if (self.isLoadingMore) {
return;
}
self.isLoadingMore = YES;
NSMutableDictionary *paramers = [NSMutableDictionary dictionaryWithCapacity:0];
[paramers setObject:[NSNumber numberWithInteger:self.startIndex ] forKey:@"start"];
[paramers setObject:[NSNumber numberWithInteger:self.pageSize] forKey:@"pageSize"];
__weak typeof(self) weakSelf = self;
[self get:@"/phone/notice/load.action" withParameters:paramers completion:^(id obj, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf hideHud];
[weakSelf.tableView endRefreshing];
});
weakSelf.isLoadingMore = NO;
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf showRefleshViewWithError:error andDelegate:weakSelf];
});
} else {
weakSelf.startIndex += weakSelf.pageSize;
[weakSelf.listArray addObjectsFromArray:[obj objectForKey:RESPONSE_DATA_KEY]];
dispatch_async(dispatch_get_main_queue(), ^{
// [weakSelf.tableView showTipViewWithText:@"暂时没有数据" dataArr:weakSelf.listArray];
[weakSelf.tableView reloadData];
});
}
}];
}
点到- (void)get:(NSString *)relativeUrlPath withParameters:(NSMutableDictionary *)parameters completion:(void (^)(id obj, NSError * error))handler
- (void)get:(NSString *)relativeUrlPath withParameters:(NSMutableDictionary *)parameters completion:(void (^)(id obj, NSError * error))handler {
return [self get:relativeUrlPath withParameters:parameters completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
handler(nil, connectionError);
} else {
[DataProsess prosess:data handler:^(id obj, NSError *error) {
if ([[obj objectForKey:RESPONSE_CODE_KEY] integerValue] != 0) {
NSString *msg = [obj objectForKey:RESPONSE_MSG_KEY];
[MessageWindow showMessage:msg];
} else {
if (error) {
handler(nil, error);
} else {
handler(obj, nil);
}
}
}];
}
}];
}
继续点 - (void)get:(NSString *)relativeUrlPath withParameters:(NSMutableDictionary *)parameters completionHandler:(void (^)(NSURLResponse *, NSData *, NSError *))handler
- (void)get:(NSString *)relativeUrlPath withParameters:(NSMutableDictionary *)parameters completionHandler:(void (^)(NSURLResponse *, NSData *, NSError *))handler {
NSURL * url = [URLUtil URLWithString:relativeUrlPath];
NSMutableURLRequest * request = [HTTPRequest HTTPGETRequestForURL:url parameters:parameters];
[URLConnection sendAsynchronousRequest:request completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithDictionary:connectionError.userInfo];
[dict setObject:CONNECTION_ERROR_DESCRIPTION forKey:NSLocalizedDescriptionKey];
NSError * error = [[NSError alloc] initWithDomain:connectionError.domain code:connectionError.code userInfo:dict];
handler(response, data, error);
} else {
[DataProsess prosess:data handler:^(id obj, NSError *error) {
if (error) {
if (error.code==-1) {
//todo 登录
}
handler(response, data, error);
} else {
handler(response, data, nil);
}
}];
}
}];
}
再点+ (void)sendAsynchronousRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLResponse *, NSData *, NSError *))handler
+ (void)sendAsynchronousRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLResponse *, NSData *, NSError *))handler {
[self sendAsynchronousRequest:request tag:0 completionHandler:handler];
}
+ (void)sendAsynchronousRequest:(NSURLRequest *)request tag:(NSInteger)tag completionHandler:(void (^)(NSURLResponse *, NSData *, NSError *))handler {
__weak typeof(self) weakSelf = self;
[self checkConnection:^(NSError *error) {
if (error) {
handler(nil, nil, error);
} else {
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//connectionError.code==-1001,请求超时
//connectionError.code==-1009,无网络连接
if (connectionError && tag<30 && connectionError.code!=-1001 && connectionError.code!=-1009) {
[NSThread sleepForTimeInterval:1];
[weakSelf sendAsynchronousRequest:request tag:tag+1 completionHandler:handler];
} else {
handler(response, data, connectionError);
}
}];
}
}];
}
继续... + (void)checkConnection:(void(^)(NSError * error))handler { dispatch_queue_t _queue = dispatch_queue_create("connection_check_queue", DISPATCH_QUEUE_SERIAL)
+ (void)checkConnection:(void(^)(NSError * error))handler {
dispatch_queue_t _queue = dispatch_queue_create("connection_check_queue", DISPATCH_QUEUE_SERIAL);
dispatch_async(_queue, ^{
if (![self localConnectionAvailable]) {
for (int i=0; i<3; i++) {
if ([self localConnectionAvailable]) {
break;
}
[NSThread sleepForTimeInterval:1];
}
}
});
dispatch_async(_queue, ^{
if ([self localConnectionAvailable]) {
if (![self isConnectionAvailable]) {
for (int i=0; i<10; i++) {
if ([self isConnectionAvailable]) {
break;
}
[NSThread sleepForTimeInterval:1];
}
}
if ([self isConnectionAvailable]) {
handler(nil);
} else {
NSDictionary * userInfo = [NSDictionary dictionaryWithObject:@"未能连接到服务器" forKey:NSLocalizedDescriptionKey];
NSError * error = [NSError errorWithDomain:@"www.51haoliao.com" code:10000001 userInfo:userInfo];
handler(error);
}
} else {
NSDictionary * userInfo = [NSDictionary dictionaryWithObject:@"请连接WiFi或打开蜂窝移动网络" forKey:NSLocalizedDescriptionKey];
NSError * error = [NSError errorWithDomain:@"www.51haoliao.com" code:10000000 userInfo:userInfo];
handler(error);
return ;
}
});
}
其中 localConnectionAvailable
+ (BOOL)localConnectionAvailable {
//创建零地址,0.0.0.0的地址表示查询本机的网络连接状态
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
// Recover reachability flags
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
//获得连接的标志
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
//如果不能获取连接标志,则不能连接网络,直接返回
if (!didRetrieveFlags)
{
return NO;
}
//根据获得的连接标志进行判断
BOOL isReachable = ((flags & kSCNetworkFlagsReachable) != 0);
BOOL needsConnection = ((flags & kSCNetworkFlagsConnectionRequired) != 0);
return (isReachable && !needsConnection) ? YES : NO;
}
再者 isConnectionAvailable
+ (BOOL)isConnectionAvailable {
//创建零地址,0.0.0.0的地址表示查询本机的网络连接状态
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
// Recover reachability flags
// SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithName(NULL, "www.51haoliao.com");
SCNetworkReachabilityFlags flags;
//获得连接的标志
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
//如果不能获取连接标志,则不能连接网络,直接返回
if (!didRetrieveFlags)
{
return NO;
}
//根据获得的连接标志进行判断
BOOL isReachable = ((flags & kSCNetworkFlagsReachable) != 0);
BOOL needsConnection = ((flags & kSCNetworkFlagsConnectionRequired) != 0);
return (isReachable && !needsConnection) ? YES : NO;
}
卖糕的...