我正在使用AFNetworking將json數據發佈到我的web服務並獲得json響應。不過,現在我也想將多部分formdata添加到這些POST。如果我這樣做(即使沒有我首先添加的參數)完成塊從不會觸發。進度塊會觸發,我可以看到文件正確上載。AFNetworking AFJSONRequestOperation和filedata永遠不會完成
有沒有人有任何經驗張貼這樣的圖像與AFNetworking?我正在使用最新的AFNetworking來源/版本。
這是我用json在其中發佈字典的初始代碼。這工作正常。
NSMutableDictionary *postdata = [[NSMutableDictionary alloc] init];
[postdata setObject:[postdictionary JSONString] forKey:@"request"];
NSMutableURLRequest *jsonRequest = [httpClient requestWithMethod:@"POST"
path:path
parameters:postdata];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:jsonRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {// Success} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {// Failure }];
[operation start];
這是代碼的提交圖片的版本(不工作)
NSMutableURLRequest *jsonRequest jsonRequest = [httpClient multipartFormRequestWithMethod:@"POST" path:path parameters:postdata constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"photo" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
[formData throttleBandwidthWithPacketSize:5000 delay:0.1];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:jsonRequest];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Upload succes");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Upload failed");
}];
[operation start];
註釋掉節流的數據(同Flickr的API),設置超時非常低。還遵循AFNetworking人員的一些提示來保留httpclient,並使用[self.httpClient enqueueHTTPRequestOperation:operation]而不是[operation start]。仍然不會通過完成塊。 我確實發現它確實有效,但我不會將filedata添加到請求中。 –
非常好,可能是服務器端的東西,想要重新檢查/發佈您的Web服務腳本? –