2016-09-24 68 views
0

当我碰到我的API也获取一些数据时,它总是给我这个错误。AFNetworkingErrorDomainCode 1011

错误域= AFNetworkingErrorDomain代码= -1011 “请求失败: 内部服务器错误(500)”

的UserInfo = {AFNetworkingOperationFailingURLResponseErrorKey = {URL:http://www.mydealdoc.com/api/v8/fencerecorddata} {状态码:500,标头{ “Cache-Control”=“no-cache”; Connection =关闭; “Content-Type”=“text/html”; 日期=“星期六,24九月2016 13:10:07 GMT”; Server =“Apache/2.4.7(Ubuntu)”; “设置Cookie”= “laravel_session = eyJpdiI6IjQ3SEtFTEt3TmN0dUFqMzZlMFB6OGZVNjdNS240NjdBVU9lSDhcLzNzeUVnPSIsInZhbHVlIjoicXlzbURmMEd5K0hlVEgwUTU0QmhjbFN0QlpNMFdXT0VGWmZrQm1jUXFDNlhVV05EK1wvTnpNUnA2WHBualpWUjRWZVkrSGRxSlBRaHpIS05MQit6SFdnPT0iLCJtYWMiOiIwYTlmYzUwNjBmNzgwYWE3MDg5MTMyMTlhN2MwYzQ5YTU3ZDAxMmQ4YThhMjU4MDFiNjAwYjYxYTQwMzdlMWQ3In0%3D; 期满=星期六,24九月2016 GMT 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); 

    }]; 

} 

回答

2

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 StackoverflowTestOC [3314:41971] { status = success; }

相关问题