//
// NetworkService.m
// AFNetworking - demo
//
// Created by liman on 14-12-2.
// Copyright (c) 2014年 liman. All rights reserved.
//
#import "NetworkService.h"
@implementation NetworkService
#pragma mark - public methods
// GCD单例
+ (NetworkService *)sharedService;
{
static NetworkService *__singletion;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__singletion = [[self alloc] init];
});
return __singletion;
}
// 请求网络数据
- (void)requestDataWithFrontURL:(NSString *)frontURL method:(NSString *)method parameter:(NSDictionary *)parameter header:(NSDictionary *)header body:(NSString *)body timeoutInterval:(NSTimeInterval)timeoutInterval result:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
// 开启状态栏菊花
[self open];
// GET方法单独特殊处理 =================================================================================
if ([method isEqualToString:@"GET"])
{
// 1.初始化
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
// 2.设置请求格式 (默认二进制, 这里不用改也OK)
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
// 3.设置超时时间
manager.requestSerializer.timeoutInterval = timeoutInterval;
// 4.设置消息头
if (header) {
for (NSString *key in [header allKeys]) {
[manager.requestSerializer setValue:[header objectForKey:key] forHTTPHeaderField:key];
}
}
// 5.设置返回格式 (默认JSON, 这里必须改为二进制)
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// 6.请求
[manager GET:frontURL parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
// 成功
success(operation, responseObject);
// 关闭状态栏菊花
[self close];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// 超时
failure(operation, error);
// 关闭状态栏菊花
[self close];
}];
}
// POST, PUT方法通用处理 =================================================================================
if ([method isEqualToString:@"POST"] || [method isEqualToString:@"PUT"])
{
// 0. 拼接完整的URL地址
NSMutableArray *laterArr = [NSMutableArray array];
for (NSString *key in [parameter allKeys]) {
NSString *keyValue = [key stringByAppendingFormat:@"=%@",[parameter objectForKey:key]];
[laterArr addObject:keyValue];
}
NSString *laterURL = [laterArr componentsJoinedByString:@"&"];
NSString *finalURL = [frontURL stringByAppendingFormat:@"?%@", laterURL];
// 1.初始化
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:finalURL]];
// 2.设置请求类型
request.HTTPMethod = method;
// 3.设置超时时间
request.timeoutInterval = timeoutInterval;
// 4.设置消息头
if (header) {
for (NSString *key in [header allKeys]) {
[request setValue:[header objectForKey:key] forHTTPHeaderField:key];
}
}
// 5.设置消息体
if (body) {
request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];
}
// 6.请求
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// 成功
success(operation, responseObject);
// 关闭状态栏菊花
[self close];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// 超时
failure(operation, error);
// 关闭状态栏菊花
[self close];
}];
[operation start];
}
}
#pragma mark - private methods
// 开启状态栏菊花
- (void)open
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
// 关闭状态栏菊花
- (void)close
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
@end