这个问题是间歇性的,我不知道为什么会这样。基本上,我试图通过向远程RESTful API发送POST请求来添加新帖子。我用POSTMAN检查了API根本没有问题,但AFNetworking似乎无缘无故地拒绝了它的一些请求。
这是我的完整代码:
- (IBAction)doneButtonPressed:(id)sender {
NSString *postBody = self.postInputField.text;
NSDictionary *params = @{@"postBody": postBody};
NetworkManager *manager = [NetworkManager sharedNetworkManager];
[manager POST:NEW_POST_API_URL parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"NewPostSuccess. resp: %@", responseObject);
[[NSNotificationCenter defaultCenter] postNotificationName:ASK_TO_ADD_POST_ROW object:responseObject userInfo:nil];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSString* ErrorResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
NSLog(@"%@", ErrorResponse);
}];
[self dismissViewControllerAnimated:YES completion:^{
[[NSNotificationCenter defaultCenter] postNotificationName:TABLE_SCROLL_TO_TOP object:nil userInfo:nil];
}];
}
这是错误:
com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x170429b40> { URL: http://192.168.0.102:3000/api/posts/new } { status code: 503, headers {
Connection = "keep-alive";
"Content-Type" = "text/plain";
Date = "Mon, 05 Oct 2015 03:15:37 GMT";
"Transfer-Encoding" = Identity;
} }, NSErrorFailingURLKey=http://192.168.0.102:3000/api/posts/new, com.alamofire.serialization.response.error.data=<556e6578 70656374 65642065 72726f72 2e>, NSLocalizedDescription=Request failed: service unavailable (503)}
task: <__NSCFLocalDataTask: 0x136e6b150>{ taskIdentifier: 10 } { completed }
间歇性地发生这种情况,我在代码中没有任何改变,它在同一个viewcontroller中再次开始工作。响应错误没用,因为它只说unexpected error
。任何指导都将非常感激。