NSURLSession in iOS getting data from network
ringa_lee
ringa_lee 2017-05-02 09:27:34
0
1
522

Can NSURLSession specify the type of data to be obtained? For example, if I want to obtain xml, but what is actually obtained is json, an error will be reported

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    [configuration setHTTPAdditionalHeaders:@{@"Accept": @"application/xml"}];
    [configuration setHTTPAdditionalHeaders:@{@"Content-Type": @"application/xml"}];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

    NSURLSessionDataTask *task = [session dataTaskWithURL:url
                                        completionHandler:^(NSData * _Nullable data,
                                                            NSURLResponse * _Nullable response,
                                                            NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@", [error localizedDescription]);
        } else {
            NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
            if (200 != statusCode) {
                NSLog(@"HTTP status code = %ld", statusCode);
                success = NO;
            }
            NSLog(@"%@", data);
        }
    }];
ringa_lee
ringa_lee

ringa_lee

reply all(1)
巴扎黑

NSURLSession does not have this function, you can try to use AFNetworking

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template