Commit 8f3a11760c93ba687ef58734160e3e5c01a56be0

Authored by 朱林
1 parent 1166942e

优化缓存策略和返回的错误信息

CNLiveNetworkingDemo/CNLiveNetworking/CNLiveNetworkManager.m
... ... @@ -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 47 return manager;
44 48 }
45 49  
... ... @@ -79,7 +83,14 @@ static NSURLRequestCachePolicy CachePolicy = NSURLRequestUseProtocolCachePolicy;
79 83 URLString:(NSString *)URLString
80 84 parameters:(NSDictionary *)parameters
81 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 95 NSString *newURLString;
85 96 if ([method isEqualToString:@"GET"]) {
... ... @@ -92,12 +103,7 @@ static NSURLRequestCachePolicy CachePolicy = NSURLRequestUseProtocolCachePolicy;
92 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 108 request.HTTPMethod = method;
103 109  
... ... @@ -122,8 +128,18 @@ static NSURLRequestCachePolicy CachePolicy = NSURLRequestUseProtocolCachePolicy;
122 128 if (error) {
123 129 failure(response,error);
124 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
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<Bucket
  3 + type = "1"
  4 + version = "2.0">
  5 +</Bucket>
... ...
CNLiveNetworkingDemo/CNLiveNetworkingDemo/ViewController.m
... ... @@ -46,7 +46,7 @@
46 46 NSDictionary * getPara = @{@"plat":@"a",
47 47 @"appid":@"hdtv",
48 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 51 CNLiveNetworkManager *manager = [CNLiveNetworkManager manager];
52 52 manager.timeoutInterval = 20 ;
... ...