Commit 8f3a11760c93ba687ef58734160e3e5c01a56be0
1 parent
1166942e
优化缓存策略和返回的错误信息
Showing
4 changed files
with
31 additions
and
10 deletions
CNLiveNetworkingDemo/CNLiveNetworking/CNLiveNetworkManager.m
| @@ -40,6 +40,10 @@ static NSURLRequestCachePolicy CachePolicy = NSURLRequestUseProtocolCachePolicy; | @@ -40,6 +40,10 @@ static NSURLRequestCachePolicy CachePolicy = NSURLRequestUseProtocolCachePolicy; | ||
| 40 | 40 | ||
| 41 | }); | 41 | }); |
| 42 | 42 | ||
| 43 | + manager.cachePolicy = NSURLRequestUseProtocolCachePolicy; | ||
| 44 | + | ||
| 45 | + manager.timeoutInterval = TimeoutInterval; | ||
| 46 | + | ||
| 43 | return manager; | 47 | return manager; |
| 44 | } | 48 | } |
| 45 | 49 | ||
| @@ -79,7 +83,14 @@ static NSURLRequestCachePolicy CachePolicy = NSURLRequestUseProtocolCachePolicy; | @@ -79,7 +83,14 @@ static NSURLRequestCachePolicy CachePolicy = NSURLRequestUseProtocolCachePolicy; | ||
| 79 | URLString:(NSString *)URLString | 83 | URLString:(NSString *)URLString |
| 80 | parameters:(NSDictionary *)parameters | 84 | parameters:(NSDictionary *)parameters |
| 81 | success:(void (^)(NSURLResponse * _Nullable, id _Nullable))success | 85 | success:(void (^)(NSURLResponse * _Nullable, id _Nullable))success |
| 82 | - failure:(void (^)(NSURLResponse * _Nullable, NSError * _Nullable))failure{ | 86 | + failure:(void (^)(NSURLResponse * _Nullable, NSError * _Nullable))failure |
| 87 | +{ | ||
| 88 | + | ||
| 89 | + if (![URLString isKindOfClass:[NSString class]]) { | ||
| 90 | + NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey : @"please enter a valid url"}]; | ||
| 91 | + failure(nil, error); | ||
| 92 | + return nil; | ||
| 93 | + } | ||
| 83 | 94 | ||
| 84 | NSString *newURLString; | 95 | NSString *newURLString; |
| 85 | if ([method isEqualToString:@"GET"]) { | 96 | if ([method isEqualToString:@"GET"]) { |
| @@ -92,12 +103,7 @@ static NSURLRequestCachePolicy CachePolicy = NSURLRequestUseProtocolCachePolicy; | @@ -92,12 +103,7 @@ static NSURLRequestCachePolicy CachePolicy = NSURLRequestUseProtocolCachePolicy; | ||
| 92 | newURLString = URLString; | 103 | newURLString = URLString; |
| 93 | } | 104 | } |
| 94 | 105 | ||
| 95 | - NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:newURLString] cachePolicy:CachePolicy timeoutInterval:TimeoutInterval]; | ||
| 96 | - | ||
| 97 | - //将缓存策略恢复默认 | ||
| 98 | - CachePolicy = NSURLRequestUseProtocolCachePolicy; | ||
| 99 | - //将超时时间恢复默认 | ||
| 100 | - TimeoutInterval = 30; | 106 | + NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:newURLString] cachePolicy:self.cachePolicy timeoutInterval:self.timeoutInterval]; |
| 101 | 107 | ||
| 102 | request.HTTPMethod = method; | 108 | request.HTTPMethod = method; |
| 103 | 109 | ||
| @@ -122,8 +128,18 @@ static NSURLRequestCachePolicy CachePolicy = NSURLRequestUseProtocolCachePolicy; | @@ -122,8 +128,18 @@ static NSURLRequestCachePolicy CachePolicy = NSURLRequestUseProtocolCachePolicy; | ||
| 122 | if (error) { | 128 | if (error) { |
| 123 | failure(response,error); | 129 | failure(response,error); |
| 124 | }else{ | 130 | }else{ |
| 125 | - id jsonObj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; | ||
| 126 | - success(response,jsonObj); | 131 | + if ([response isKindOfClass:[NSHTTPURLResponse class]]) { |
| 132 | + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; | ||
| 133 | + if (httpResponse.statusCode < 400) { | ||
| 134 | + id jsonObj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; | ||
| 135 | + success(httpResponse,jsonObj); | ||
| 136 | + } else { | ||
| 137 | + NSError *failureError = [NSError errorWithDomain:NSURLErrorDomain code:httpResponse.statusCode userInfo:httpResponse.allHeaderFields]; | ||
| 138 | + failure(httpResponse,failureError); | ||
| 139 | + } | ||
| 140 | + } else { | ||
| 141 | + failure(response,error); | ||
| 142 | + } | ||
| 127 | } | 143 | } |
| 128 | }); | 144 | }); |
| 129 | }]; | 145 | }]; |
CNLiveNetworkingDemo/CNLiveNetworkingDemo.xcodeproj/project.xcworkspace/xcuserdata/android.xcuserdatad/UserInterfaceState.xcuserstate
No preview for this file type
CNLiveNetworkingDemo/CNLiveNetworkingDemo.xcodeproj/xcuserdata/android.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist
0 → 100644
CNLiveNetworkingDemo/CNLiveNetworkingDemo/ViewController.m
| @@ -46,7 +46,7 @@ | @@ -46,7 +46,7 @@ | ||
| 46 | NSDictionary * getPara = @{@"plat":@"a", | 46 | NSDictionary * getPara = @{@"plat":@"a", |
| 47 | @"appid":@"hdtv", | 47 | @"appid":@"hdtv", |
| 48 | @"version":@"5.3"}; | 48 | @"version":@"5.3"}; |
| 49 | - NSString *gettUrl = @"http://intertv.cnlive.com/api/activity"; | 49 | + NSString *gettUrl = @"http://intertv.cnlive.com/api/activity11111111"; |
| 50 | 50 | ||
| 51 | CNLiveNetworkManager *manager = [CNLiveNetworkManager manager]; | 51 | CNLiveNetworkManager *manager = [CNLiveNetworkManager manager]; |
| 52 | manager.timeoutInterval = 20 ; | 52 | manager.timeoutInterval = 20 ; |