Skip to content

Commit

Permalink
修改网络请求框架
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank authored and Frank committed Nov 8, 2017
1 parent b5ac9ed commit c4a1a53
Show file tree
Hide file tree
Showing 18 changed files with 1,115 additions and 174 deletions.
Binary file modified .DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
01A36B5D1FB0484700B1893A /* 1.png in Resources */ = {isa = PBXBuildFile; fileRef = 01A36B5C1FB0483700B1893A /* 1.png */; };
01BEE1AC1EEEEB14002C2D79 /* HttpModelDelegateTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 01BEE1AB1EEEEB14002C2D79 /* HttpModelDelegateTest.m */; };
01BEE1AF1EEEEB32002C2D79 /* HttpModelBlockTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 01BEE1AE1EEEEB32002C2D79 /* HttpModelBlockTest.m */; };
01C050B01EEE869C0020160F /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 01C050AF1EEE869C0020160F /* main.m */; };
Expand Down Expand Up @@ -49,6 +50,9 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
01A36B5C1FB0483700B1893A /* 1.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = 1.png; sourceTree = "<group>"; };
01A36B5F1FB2D49400B1893A /* XMLDictionary.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XMLDictionary.h; sourceTree = "<group>"; };
01A36B601FB2D49400B1893A /* XMLDictionary.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = XMLDictionary.m; sourceTree = "<group>"; };
01BEE1AA1EEEEB14002C2D79 /* HttpModelDelegateTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HttpModelDelegateTest.h; sourceTree = "<group>"; };
01BEE1AB1EEEEB14002C2D79 /* HttpModelDelegateTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HttpModelDelegateTest.m; sourceTree = "<group>"; };
01BEE1AD1EEEEB32002C2D79 /* HttpModelBlockTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HttpModelBlockTest.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -123,6 +127,15 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
01A36B5E1FB2D49400B1893A /* XMLDictionary */ = {
isa = PBXGroup;
children = (
01A36B5F1FB2D49400B1893A /* XMLDictionary.h */,
01A36B601FB2D49400B1893A /* XMLDictionary.m */,
);
path = XMLDictionary;
sourceTree = "<group>";
};
01C050A21EEE869C0020160F = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -153,6 +166,7 @@
01C050B21EEE869D0020160F /* AppDelegate.m */,
01C050B41EEE869D0020160F /* ViewController.h */,
01C050B51EEE869D0020160F /* ViewController.m */,
01A36B5C1FB0483700B1893A /* 1.png */,
01BEE1AA1EEEEB14002C2D79 /* HttpModelDelegateTest.h */,
01BEE1AB1EEEEB14002C2D79 /* HttpModelDelegateTest.m */,
01BEE1AD1EEEEB32002C2D79 /* HttpModelBlockTest.h */,
Expand Down Expand Up @@ -196,6 +210,7 @@
01C050E21EEE8B470020160F /* FrankHttpRequestManage */ = {
isa = PBXGroup;
children = (
01A36B5E1FB2D49400B1893A /* XMLDictionary */,
01C050E31EEE8B470020160F /* FrankAPIClicent.h */,
01C050E41EEE8B470020160F /* FrankAPIClicent.m */,
01C050E71EEE8B470020160F /* FrankNetworkManage.h */,
Expand Down Expand Up @@ -372,6 +387,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
01A36B5D1FB0484700B1893A /* 1.png in Resources */,
01C050BE1EEE869D0020160F /* LaunchScreen.storyboard in Resources */,
01C050BB1EEE869D0020160F /* Assets.xcassets in Resources */,
01C050B91EEE869D0020160F /* Main.storyboard in Resources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,61 @@
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "FrankHttpRequestManegerDemo/AppDelegate.m"
timestampString = "519009996.004108"
filePath = "FrankHttpRequestManegerDemo/FrankHttpRequestManage/FrankNetworkManage.m"
timestampString = "531815988.130544"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "38"
endingLineNumber = "38"
landmarkName = "-setHttpBaseMessage"
startingLineNumber = "299"
endingLineNumber = "299"
landmarkName = "-completionHandlerWithTask:responseData:requestParams:error:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "FrankHttpRequestManegerDemo/FrankHttpRequestManage/FrankNetworkManage.m"
timestampString = "531815988.130646"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "298"
endingLineNumber = "298"
landmarkName = "-completionHandlerWithTask:responseData:requestParams:error:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "FrankHttpRequestManegerDemo/FrankHttpRequestManage/FrankNetworkManage.m"
timestampString = "531815988.130705"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "295"
endingLineNumber = "295"
landmarkName = "-completionHandlerWithTask:responseData:requestParams:error:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "FrankHttpRequestManegerDemo/FrankHttpRequestManage/FrankNetworkManage.m"
timestampString = "531815988.130757"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "254"
endingLineNumber = "254"
landmarkName = "+httpRequestWithHttpMethod:responseDataType:urlString:headerParams:params:finally:sucess:failure:error:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,42 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
return YES;
}

// ------ 根据自己服务器数据格式配置请求结果判断的回调 ------------
#error ------ 根据自己服务器数据格式配置请求结果判断的回调 ------------

/**
配置网络请求
*/
-(void)setHttpBaseMessage{

// 配置网络请求 baseUrl
[FrankNetworkManage updateBaseUrl:@"http://apistore.baidu.com/"];
[FrankNetworkManage updateBaseUrl:@"https://news-at.zhihu.com/"];

// 配置网络数据缓存类型
[FrankNetworkManage shareManager].cacheType = NetworkCacheType_CacheAndLoad;
[FrankNetworkManage shareManager].cacheType = NetworkCacheType_OnlyCache;

// 配置网络请求成功判断逻辑
[FrankNetworkManage shareManager].judgeResponseIsSuccess = ^BOOL(id responseSuccess) {

BOOL loginSucess = NO;

if ( [responseSuccess isKindOfClass:[NSDictionary class]] ) {

if (responseSuccess[@"errNum"] != nil) {

// 判断是否请求成功,loginSucess = YES :表示成功,否则表示失败
loginSucess = [responseSuccess[@"errNum"] isEqualToNumber:@(0)];

// 账号被登出,下次需要重新登录,并且发送通知
if ([responseSuccess[@"errNum"] isEqualToNumber:@(4)]) {

[[NSNotificationCenter defaultCenter] postNotificationName:@"账号失效通知" object:nil userInfo:responseSuccess];// 发送重新登录的通知
}
}
}
return loginSucess;
};

#error ----- 如果 服务器 返回的有 成功状态标识,可以根据状态进行 如下 配置,否则不需要配置 judgeResponseIsSuccess

// // 配置网络请求成功判断逻辑
// [FrankNetworkManage shareManager].judgeResponseIsSuccess = ^BOOL(id responseSuccess) {
//
// BOOL loginSucess = NO;
//
// if ( [responseSuccess isKindOfClass:[NSDictionary class]] ) {
//
// // 根据服务器定制的成功状态进行配置,loginSucess = YES :表示成功,否则表示失败
// loginSucess = [responseSuccess[@"code"] isEqualToNumber:@(200)];
//
// // 根据自己项目需求,如果服务器定义字段,进行判断是否需要发出区分账号失效通知及相关处理
// // 账号被登出,下次需要重新登录,并且发送通知
// if ([responseSuccess[@"code"] isEqualToNumber:@(401)]) {
//
// [[NSNotificationCenter defaultCenter] postNotificationName:@"账号失效通知" object:nil userInfo:responseSuccess];// 发送重新登录的通知
// }
//
// }
// return loginSucess;
// };

}

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ typedef NS_ENUM(NSInteger,BusinessHttpType) {
请求头数据字典
*/
@property (nonatomic,strong)NSMutableDictionary * headerParams;
/**
@return 类方法获取去求头数据
*/
+ (NSDictionary *)headerParams;

/**
网络请求失败回调
Expand All @@ -79,7 +83,6 @@ typedef NS_ENUM(NSInteger,BusinessHttpType) {
*/
@property (nonatomic,copy)ReplyError networkerrBlock;

#pragma mark -------- 通过 代理 方式处理请求回调 ---------
#pragma mark -------- 通过 代理 方式处理请求回调 ---------

/**
Expand Down Expand Up @@ -107,7 +110,7 @@ typedef NS_ENUM(NSInteger,BusinessHttpType) {
-(void)doRequestWithHttpMethod:(HTTP_REQUEST_METHOD)method
params:(id)params
isNeedHeaderParams:(BOOL)isNeedHeader;
#pragma mark -------- 通过 block 方式处理请求回调 ---------

#pragma mark -------- 通过 block 方式处理请求回调 ---------

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,35 @@ -(instancetype)init

FrankLog(@"error");

//#error ------ 根据自己服务器返回的数据结构进行解析处理 ------------

NSString * value = [[error userInfo] objectForKey:NSLocalizedDescriptionKey];
// NSString * value = [[error userInfo] objectForKey:NSLocalizedDescriptionKey];

NSString * errStr = [[error userInfo] objectForKey:@"NSDebugDescription"];

if (value)
{
[FrankActivityHUD showWithText:errStr?errStr:@"网络异常,请稍后再试" shimmering:NO];

}else{
[FrankActivityHUD showWithText:@"服务器异常,请稍后再试" shimmering:NO];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

NSString *noteStr = nil;
if ((status == AFNetworkReachabilityStatusUnknown) || (status == AFNetworkReachabilityStatusNotReachable)) {
noteStr = @"当前网络不可用,请检查您的网络设置";
} else {
noteStr = @"连接服务器失败,请稍后重试";
}

}
dispatch_async(dispatch_get_main_queue(), ^{

[FrankActivityHUD showWithText:errStr?errStr:noteStr shimmering:NO];
});

}];

};


self.failureBlock = ^(NSURLSessionDataTask *task, id responseObject, NSDictionary *requestParams) {

//#error ------ 根据自己服务器返回的数据结构进行解析处理 ------------

// 处理请求失败时,显示服务器返回的失败原因
FrankLog(@"%@",responseObject);
NSString * msg = responseObject[@"errMsg"];
if ([msg isEqualToString:@"token or code fail"]) {
msg = @"该账号验证信息失效,需重新登录验证";
}

if (msg) {

Expand All @@ -72,27 +75,26 @@ -(NSMutableDictionary *)headerParams
}
[_headerParams removeAllObjects];// 移除所有数据

//#error ------ 根据自己服务器要求配置请求头信息 ------------
#error ------ 根据自己服务器要求配置请求头信息 ------------

NSString * token = [[FrankUserDefaults share] FrankObjectForKey:@"token"];

NSString * code = [[FrankUserDefaults share] FrankObjectForKey:@"code"];
[_headerParams setValue:@"value" forKey:@"key"];

return _headerParams;
}
/**
@return 类方法获取去求头数据
*/
+ (NSDictionary *)headerParams{

if (token) {
[_headerParams setObject:token forKey:@"token"];
}
if (code) {
[_headerParams setObject:code forKey:@"code"];
}
NSMutableDictionary * dic = [[NSMutableDictionary alloc]initWithCapacity:0];

if (_headerParams.count == 2) {

return _headerParams;
}
#error ------ 根据自己服务器要求配置请求头信息 ------------

[dic setValue:@"value" forKey:@"key"];

return nil;
return dic;
}
#pragma mark -------- 通过 代理 方式处理请求回调 ---------

#pragma mark -------- 通过 代理 方式处理请求回调 ---------

-(void)doneBusiness:(ResponseHttpType)returnStatus
Expand All @@ -116,7 +118,6 @@ -(void)doRequestWithHttpMethod:(HTTP_REQUEST_METHOD)method params:(id)params isN
[self doRequestWithHttpMethod:method params:params isNeedHeaderParams:isNeedHeader success:nil failure:nil netError:nil];
}
#pragma mark -------- 通过 block 方式处理请求回调 ---------
#pragma mark -------- 通过 block 方式处理请求回调 ---------
/**
供子类调用
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@

+ (instancetype)sharedClient;

/**
对 JSON 响应数据的配置
*/
- (void)responseForJson;
/**
对 XML 响应数据的配置
*/
- (void)responseForXML;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,33 @@ + (instancetype)sharedClient
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[self alloc] initWithBaseURL:nil];

_sharedClient.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"text/json", @"text/html", @"text/javascript", nil];


_sharedClient.requestSerializer.HTTPShouldHandleCookies = YES;
[_sharedClient.requestSerializer willChangeValueForKey:@"timeoutInterval"];
_sharedClient.requestSerializer.timeoutInterval = 30.f;
[_sharedClient.requestSerializer didChangeValueForKey:@"timeoutInterval"];

[_sharedClient.operationQueue setMaxConcurrentOperationCount:1];

/// 移除服务器返回的 Null 值,保证系统数据值的安全 ;同样可以使用 NullSafe 库进行处理
((AFJSONResponseSerializer *)_sharedClient.responseSerializer).removesKeysWithNullValues = YES;


});
return _sharedClient;
}

- (void)responseForJson{

self.responseSerializer = [AFJSONResponseSerializer serializer];

self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"text/json", @"text/html", @"text/javascript", nil];

/// 移除服务器返回的 Null 值,保证系统数据值的安全 ;同样可以使用 NullSafe 库进行处理
((AFJSONResponseSerializer *)self.responseSerializer).removesKeysWithNullValues = YES;
}
- (void)responseForXML{

self.responseSerializer = [AFXMLParserResponseSerializer serializer];
self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/xml", @"text/xml", @"text/javascript", @"text/html", nil];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -755,9 +755,6 @@ -(BOOL)cacheNetWorkDataWithTableName:(NSString *)tableName paramsKey:(NSDictiona
if (!tableName) {
NSLog(@"---%@ 创建表失败--表名不能为空",tableName);
return NO;
}else if (!paramsKey || paramsKey.count<=0){
NSLog(@"---%@ 创建表失败--字段数组不能为空",paramsKey);
return NO;
}

tableName = [FrankFMDBManage replaceStringForOriginStr:tableName replaceArray:@[@"/",@":",@"."] toString:@""];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
#import "FrankUserDefaults.h"
#import "NSString+Category.h"
#import "DeviceManage.h"

#import "XMLDictionary.h"

#endif /* FrankNetworkMacro_h */
Loading

0 comments on commit c4a1a53

Please sign in to comment.