AFNetworking中的响应标头和URL

时间:2015-05-06 11:27:51

标签: ios objective-c cocoa-touch afnetworking-2 nsurlsession

我正在使用AFNetworking将视频和图像上传到服务器。

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:urlString parameters:dictParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imgData name:@"photo" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:[NSData dataWithContentsOfFile:pathVideo] name:@"video" fileName:@"video.mp4" mimeType:@"video/mp4"];
    } error:nil];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
NSProgress *progress = nil;

NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"%@ %@", response, responseObject);
    }
}];

[uploadTask resume];

但是在回复中我得到了响应标题和URL以及所需的响应。

{ URL: the_url.com } { status code: 200, headers {
    Connection = "Keep-Alive";
    "Content-Length" = 49;
    "Content-Type" = "text/html; charset=UTF-8";
    Date = "Wed, 06 May 2015 11:12:50 GMT";
    "Keep-Alive" = "timeout=5, max=100";
    Server = "Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips PHP/5.6.7";
    "X-Powered-By" = "PHP/5.6.7";
} } {
    error = 0;
    message = "Test Button";
    response =     (
    );
}

我已经搜索了很多,但找不到任何有用的东西。请帮帮我。

1 个答案:

答案 0 :(得分:1)

您正在使用真实响应记录标题:

NSLog(@"%@ %@", response, responseObject);

您希望的响应只是responseObject,显然是一个字典。

NSLog(@"%@", responseObject);

例如,您可以访问&#34;消息&#34;做:

NSLog(@"%@", responseObject[@"message"]);

应记录:&#34;测试按钮&#34;