AFNetworkingErrorDomainCode 1011

AFNetworkingErrorDomainCode 1011

每当我点击我的 API 获取一些数据时,它总是给我这个错误。

Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)"

UserInfo={AFNetworkingOperationFailingURLResponseErrorKey= { URL: http://www.mydealdoc.com/api/v8/fencerecorddata } { status code: 500, headers { "Cache-Control" = "no-cache"; Connection = close; "Content-Type" = "text/html"; Date = "Sat, 24 Sep 2016 13:10:07 GMT"; Server = "Apache/2.4.7 (Ubuntu)"; "Set-Cookie" = "laravel_session=eyJpdiI6IjQ3SEtFTEt3TmN0dUFqMzZlMFB6OGZVNjdNS240NjdBVU9lSDhcLzNzeUVnPSIsInZhbHVlIjoicXlzbURmMEd5K0hlVEgwUTU0QmhjbFN0QlpNMFdXT0VGWmZrQm1jUXFDNlhVV05EK1wvTnpNUnA2WHBualpWUjRWZVkrSGRxSlBRaHpIS05MQit6SFdnPT0iLCJtYWMiOiIwYTlmYzUwNjBmNzgwYWE3MDg5MTMyMTlhN2MwYzQ5YTU3ZDAxMmQ4YThhMjU4MDFiNjAwYjYxYTQwMzdlMWQ3In0%3D; expires=Sat, 24-Sep-2016 15:10:07 GMT; Max-Age=7200; path=/; httponly"; "X-Frame-Options" = SAMEORIGIN; "X-Powered-By" = "PHP/5.5.9-1ubuntu4.9"; } }, NSLocalizedDescription=Request failed: internal server error (500), NSErrorFailingURLKey=http://www.mydealdoc.com/api/v8/fencerecorddata}

我有 google 这个问题,他们都用这个解决方案回答我应该将响应序列化程序设置为 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);

    }];

}

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