在iOS 7中获取错误“Error Domain = com.alamofire.error.serialization.response Code = -1011”请求失败:错误请求(400)

问题描述 投票:1回答:1

我正在使用AFNetworking版本“2.5.4”并创建多部分数据请求。在我的情况下,代码在iOS8上工作正常,但在iOS 7上出现问题。获取错误

“Error Domain = com.alamofire.error.serialization.response Code = -1011”请求失败:错误请求(400)“

UserInfo = 0x19039c00 {com.alamofire.serialization.response.error.response = {URL:http://xxx/api/abc/PostApi} {status code:400,headers {Connection = close; “Content-Type”=“application / json”;日期=“星期三,2015年5月20日05:42:47 GMT”;服务器= Apache; },NSErrorFailingURLKey = http://xxx/api/abc/PostApi},NSLocalizedDescription =请求失败:错误请求(400),com.alamofire.serialization.response.error.data = <7b227375 63636573 73223a66 616c7365 2c226d65 73736167 65223a22 4e6f2064 61746122 2c226461 7461223a 5b5d2c22 72657370 6f6e7365 5f636f64 65223a22 52433030 3033227d >}

这是我的代码有问题

`

@property (strong, nonatomic) NSURLSessionUploadTask *postUploadTask;

__block int i=1;
            AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:urlString parameters:postParameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if (imageArray.count > 0) {
            long dataLenght = 0.0;
            for(UIImage *eachImage in imageArray)
            {
                NSData *imageData = UIImageJPEGRepresentation(eachImage, .5);
                dataLenght = dataLenght + imageData.length;
                [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"image%d",i] fileName:[NSString stringWithFormat:@"image%d.jpg",i] mimeType:@"image/jpeg"];
                i++;
            }

        }
    } error:nil];

    NSProgress *progress = nil;
    self.postUploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        [progress removeObserver:object forKeyPath:kProgressFractionCompleted context:kPostUpdateTypePostUpdate];
        completionBlock(responseObject, error, task);
    }];
    [self.postUploadTask resume];` 

虽然这是在同一个api上工作的代码:

     AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL]];

    AFHTTPRequestOperation *op = [manager POST:urlString parameters:postParameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if (imageArray.count > 0) {
            long dataLenght = 0.0;
            for(UIImage *eachImage in imageArray)
            {
                NSData *imageData = UIImageJPEGRepresentation(eachImage, .5);
                dataLenght = dataLenght + imageData.length;
                [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"image%d",i] fileName:[NSString stringWithFormat:@"image%d.jpg",i] mimeType:@"image/jpeg"];
                i++;
            }
        }
    } success:^(AFHTTPRequestOperation *operation, id responseObject){

        completionBlock(responseObject, nil, task);

        [progress removeObserver:object forKeyPath:kProgressFractionCompleted context:kPostUpdateTypePostUpdate];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        completionBlock(operation, error, task);

    }];
    [op start];

但我的要求是实现uploadTaskWithStreamedRequest。这类似于问题“Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)”。

如果有任何想法,请帮助我们。提前致谢。

ios afnetworking multipartform-data
1个回答
0
投票

您应该尝试将请求序列化程序添加到AFHTTPRequestOperationManager。

    AFJSONRequestSerializer *jsonRequestSerializer = [AFJSONRequestSerializer serializer];
    [self.requestOperationManager setRequestSerializer:jsonRequestSerializer];
© www.soinside.com 2019 - 2024. All rights reserved.