Skip to content

Commit e676826

Browse files
committed
增加取消网络请求报错处理。优化结构
1 parent 2c5bf99 commit e676826

39 files changed

+151
-141
lines changed

MYNetWorkingDemo.xcodeproj/project.pbxproj

Lines changed: 128 additions & 120 deletions
Large diffs are not rendered by default.

MYNetWorkingDemo/NetWorking/MYApiRequestBaseObject.m renamed to MYNetWorkingDemo/NetWorking/MYNetWorking/MYApiRequestBaseObject.m

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@ + (instancetype)creatRequest:(MY_APIREQUEST_TYPE)requestType urlString:(NSString
2828

2929
- (instancetype)initWithRequest:(MY_APIREQUEST_TYPE)requestType urlString:(NSString *)urlString timeout:(NSInteger)timeout requestTag:(NSString *)tag
3030
{
31-
#ifdef DEBUG
31+
//requestTag可以不设定,每次均为不同的请求,此时requestTag为当前时间戳
3232
if (!tag.length) {
33-
// HudShowMessage(@"tag必须设定",[UIApplication sharedApplication].keyWindow, 2);
34-
return nil;
33+
tag = MYGetCurrentTime();
3534
}
36-
#endif
3735
MYApiRequestBaseObject *request = [self init];
3836
request.urlString = urlString;
3937
request.requestType = requestType;
@@ -42,24 +40,15 @@ - (instancetype)initWithRequest:(MY_APIREQUEST_TYPE)requestType urlString:(NSStr
4240
return request;
4341
}
4442

45-
- (instancetype)init
46-
{
47-
if (self = [super init]) {
48-
49-
_token = @"获取的本地token";
50-
_machine_type = @"iOS";
51-
}
52-
return self;
53-
}
5443

55-
//忽略
44+
5645
+ (NSArray *)modelPropertyBlacklist {
57-
return @[@"urlString", @"requestType",@"baseUrl",@"timeout",@"localCache",@"task"];
46+
return @[@"urlString", @"requestType",@"baseUrl",@"timeout",@"task",@"requestTag",@"localCache"];
5847
}
5948

6049
- (NSString *)description
6150
{
62-
return [NSString stringWithFormat:@"baseUrl ************* %@\n请求方式 ************* %ld\n完整url ************* %@\n设置超时时间 ************* %ld",self.baseUrl,(long)self.requestType,self.task.originalRequest.URL.absoluteString,(long)self.timeout];
51+
return [NSString stringWithFormat:@"baseUrl ************* %@\n请求方式 ************* %ld\n完整url ************* %@\n设置超时时间 ************* %ld\n请求参数 ************* %@",self.baseUrl,(long)self.requestType,self.task.originalRequest.URL.absoluteString,(long)self.timeout,self.yy_modelToJSONObject];
6352
}
6453

6554
- (NSInteger)timeout
@@ -91,4 +80,10 @@ - (NSString *)urlString
9180
return _urlString;
9281
}
9382

83+
//获取当前时间戳
84+
NS_INLINE NSString *MYGetCurrentTime() {
85+
UInt64 recordTime = [[NSDate date] timeIntervalSince1970];
86+
return [NSString stringWithFormat:@"%llu",recordTime];
87+
}
88+
9489
@end

MYNetWorkingDemo/NetWorking/MYApiResponseObject.m renamed to MYNetWorkingDemo/NetWorking/MYNetWorking/MYApiResponseObject.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ + (instancetype)creatResponse:(id)responsObj error:(NSError *)error task:(NSURLS
2020
response = [self new];
2121
}
2222
NSInteger httpCode = [[task.response valueForKey:@"statusCode"] integerValue];
23-
if (error) {
24-
response.tipMsg = @"网络错误";
23+
response.httpCode = httpCode;
24+
if (httpCode != 200) { //取消请求
25+
if (error.code == -999) {
26+
response.tipMsg = @"";
27+
}else{
28+
response.tipMsg = @"网络错误";
29+
}
2530
response.errorDesc = error.localizedDescription;
2631
}else{
2732
if (response.code) {

0 commit comments

Comments
 (0)