objective-c - iOS中的NSURLSession从网络获取数据
ringa_lee
ringa_lee 2017-05-02 09:27:34
0
1
490

NSURLSession能不能规定获取数据的类型,比如说我希望获取的是xml,但是实际获取的却是json,这样就会报错

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

membalas semua(1)
巴扎黑

NSURLSession tidak mempunyai fungsi ini, anda boleh cuba menggunakan AFNetworking

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan