我正在使用AFNetworking 2.2.3
,AFNetworking+AutoRetry 0.0.3
和AFKissXMLRequestOperation@aceontech 0.0.4
。
我有以下代码从服务器获取数据:
+ (void)doNetwork {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFKissXMLResponseSerializer serializer];
NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:@"someValue", @"someKey", nil];
[manager POST:@"http://example.com/api/" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError *error;
DDXMLDocument *xml = [[DDXMLDocument alloc] initWithXMLString:operation.responseString options:0 error:&error];
if(error != nil) {
NSLog(@"Error Parsing XML");
[[NSNotificationCenter defaultCenter] postNotificationName:@"FetchAPINotification" object:nil];
} else {
NSString *xPath = @"response/status";
NSArray *arr_status = [xml nodesForXPath:xPath error:nil];
if(arr_status == nil || arr_status.count == 0) {
NSLog(@"Status Not Found");
[[NSNotificationCenter defaultCenter] postNotificationName:@"FetchAPINotification" object:nil];
} else {
int status = [[arr_status objectAtIndex:0] intValue];
if(status == 0) { // OK
[[NSNotificationCenter defaultCenter] postNotificationName:@"FetchAPINotification" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"OK", @"status", nil];
} else if(status == 123) { // Requires Re-login
[self doLogin];
// How should I call the method again?
[self doNetwork];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:@"FetchAPINotification" object:nil];
}
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Network Error");
[[NSNotificationCenter defaultCenter] postNotificationName:@"FetchAPINotification" object:nil];
} autoRetry:3];
}
以下是解释:
首先,我使用AFHTTPRequestOperationManager
和param
发出HTTP POST请求。然后,在成功阻止中,如果status = 0,则向预定义的通知观察者发布通知以标记成功。
如果有任何错误,我发布没有userInfo
的通知,以标记操作失败。
但是,有一种情况是服务器响应status = 123,这意味着用户令牌已过期,必须重新登录才能刷新其令牌。
我的问题是:重新登录后如何重新尝试操作?
注意:我不是在谈论网络超时重试,我已经实现了。