请求失败:未授权(401)AFNetwoking如何解决此问题

时间:2016-03-22 08:01:53

标签: ios object

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSString *URLString = @"http://162.243.9.247:1337/login";
NSURL*url=[NSURL URLWithString:URLString];
NSDictionary *parameters = @{@"email": @"password", @"baz": @[@1, @2, @3]};
NSMutableURLRequest*request=[[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters error:nil];

manager.responseSerializer=[AFHTTPResponseSerializer serializer];

[[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters error:nil];

NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        //NSLog(@"Error: %@", error);
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error" message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:ok];

        [self presentViewController:alertController animated:YES completion:nil];

    } else {

        NSArray *responseArray = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@ ----->%@", response, responseArray[0][@"lfs"]);
        self.fullarray = responseArray[0][@"lfs"];
    }
}];

[dataTask resume];

1 个答案:

答案 0 :(得分:0)

确保

Make sure that these added in Plist

服务器上传步骤

  NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

 NSString *urlType = @"http://162.243.9.247:1337/login";
NSDictionary *param = @{@"email":@"one_user@gmail.com", @"password":@"asdfasdf"};

NSMutableURLRequest*request=[[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlType parameters:param error:nil];


[[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters error:nil];

NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        //NSLog(@"Error: %@", error);

    } else {

        NSArray *responseArray = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@ ----->%@", response, responseArray[0][@"lfs"]);
        self.fullarray = responseArray[0][@"lfs"];
    }
}];

[dataTask resume];