AFNetworkingErrorDomainCode 1011
Posted
技术标签:
【中文标题】AFNetworkingErrorDomainCode 1011【英文标题】: 【发布时间】:2016-09-24 13:17:26 【问题描述】:每当我使用我的 API 获取一些数据时,它总是给我这个错误。
错误域=AFNetworkingErrorDomain 代码=-1011 “请求失败: 内部服务器错误 (500)"
UserInfo=AFNetworkingOperationFailingURLResponseErrorKey= URL: http://www.mydealdoc.com/api/v8/fencerecorddata 状态码:500,标题 “缓存控制”=“无缓存”; 连接=关闭; “内容类型”=“文本/html”; 日期 =“2016 年 9 月 24 日星期六 13:10:07 GMT”; 服务器 = "Apache/2.4.7 (Ubuntu)"; “的Set-Cookie”=“laravel_session = eyJpdiI6IjQ3SEtFTEt3TmN0dUFqMzZlMFB6OGZVNjdNS240NjdBVU9lSDhcLzNzeUVnPSIsInZhbHVlIjoicXlzbURmMEd5K0hlVEgwUTU0QmhjbFN0QlpNMFdXT0VGWmZrQm1jUXFDNlhVV05EK1wvTnpNUnA2WHBualpWUjRWZVkrSGRxSlBRaHpIS05MQit6SFdnPT0iLCJtYWMiOiIwYTlmYzUwNjBmNzgwYWE3MDg5MTMyMTlhN2MwYzQ5YTU3ZDAxMmQ4YThhMjU4MDFiNjAwYjYxYTQwMzdlMWQ3In0%3D; 到期=格林威治标准时间 2016 年 9 月 24 日星期六 15:10:07;最大年龄=7200;路径=/; httponly"; “X-Frame-Options”=SAMEORIGIN; "X-Powered-By" = "php/5.5.9-1ubuntu4.9"; , NSLocalizedDescription=请求失败:内部服务器错误(500), NSErrorFailingURLKey=http://www.mydealdoc.com/api/v8/fencerecorddata
我有这个问题的谷歌,他们都用这个解决方案回答我应该将响应序列化程序设置为 JSON。 但是我已经设置好了。
这是我的代码。
-(void)userHasCheckThisStoreDealsWithStoreName:(NSArray *)data
NSString *storeName = [data objectAtIndex:0];
NSString *branchName = [data objectAtIndex:1];
NSString *userID = [data objectAtIndex:2];
NSString *check = [data objectAtIndex:3];
NSLog(@"Store Name: %@",storeName);
NSDictionary *param = @@"user_id":userID ,@"s_name":storeName , @"branch_name":branchName, @"enter_flag":check;
[BabyNetworkManager postWithUrlString:@"http://www.mydealdoc.com/api/v8/fencerecorddata" parameters:param success:^(id data)
NSError *error;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];;
NSLog(@"Dictionary = %@",responseDictionary);
if ([[responseDictionary objectForKey:@"status"] isEqualToString:@"success"])
else
NSArray *arrayOfParamObjects = [NSArray arrayWithObjects: storeName,userID,branchName,check, nil];
[self performSelectorInBackground:@selector(userHasCheckThisStoreDealsWithStoreName:) withObject:arrayOfParamObjects];
failure:^(NSError *error)
NSLog(@"Error %@",error);
];
请阅读我在下面函数的第 3 行中提到的评论。
+(void)postWithUrlString:(NSString *)urlString parameters:(NSDictionary *)parameters success:(HttpSuccess)success failure:(HttpFailure)failure
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//
manager.requestSerializer = [AFJSONRequestSerializer serializer];// I have check this line by commenting and uncommenting in both state i get the same error
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil];
[manager POST:urlString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
success(responseObject);
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
failure(error);
];
【问题讨论】:
【参考方案1】:HTTP 500 是服务器错误,我猜这个错误是由你发送到服务器的invalid post param
引起的,服务器端发生了一些异常。
因为我已经在 OC 中尝试过您的 API,所以我可以得到正确的 JSON 响应。示例代码如下:
ApiClient.m
#import "ApiClient.h"
@implementation ApiClient
+ (instancetype)sharedClient
static ApiClient *_sharedClient;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
_sharedClient = [[ApiClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.mydealdoc.com"]];
_sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
);
return _sharedClient;
@end
ViewController.m
#import "ViewController.h"
#import "ApiClient.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
NSDictionary *param = @@"user_id":@"123" ,@"s_name":@"123" , @"branch_name":@"123", @"enter_flag":@"123";
[[ApiClient sharedClient] POST:@"/api/v8/fencerecorddata" parameters:param constructingBodyWithBlock:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
NSLog(@"%@", [responseObject description]);
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
NSLog(@"%@", [error localizedDescription]);
];
@end
这是控制台日志:
2016-09-25 11:14:01.588 ***TestOC[3314:41971] 状态=成功;
【讨论】:
以上是关于AFNetworkingErrorDomainCode 1011的主要内容,如果未能解决你的问题,请参考以下文章