2

问题

我的应用程序允许用户上传照片。这很好用。

现在,如果照片上传失败(例如由于连接速度慢),我正在尝试实现“重试”功能。

这是我的重试代码:

self.operation = [self.operation copy];  // Creates a new operation with the same NSURLRequest

[self.operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    // do success stuff

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

    NSLog("%@", error);

}];

[[MyAFHTTPClient sharedClient] enqueueHTTPRequestOperation:self.operation];

启动时,调用失败块,输出:

$0 = 0x12636b50 Error Domain=NSURLErrorDomain Code=-1021 "request body stream exhausted" UserInfo=0x12637810 {NSErrorFailingURLStringKey=https://my/long/url/, NSErrorFailingURLKey=https://my/long/url/, NSLocalizedDescription=request body stream exhausted, NSUnderlyingError=0x13046bb0 "request body stream exhausted"}

问题

如何更改我的代码以正确重新启动图像上传?

我试过的

我认为问题在于operation.request.HTTPBodyStreamNSInputStream它无法重新启动。

该方法-[AFURLConnectionOperation connection:needNewBodyStream:]似乎提供了输入流的副本。我在那里设置了一个断点;复制或启动操作时不会调用它,我不知道如何触发它。

在 AFNetworking GitHub 页面上有一些关于类似问题的讨论,但这与身份验证失败后的重试有关。

其他信息

我的 URL 请求对象是使用创建的-[AFHTTPClient multipartFormRequestWithMethod: path: parameters: constructingBodyWithBlock:]

4

1 回答 1

1

我会尝试这样的事情:

-(void)uploadImage:(NSData *)imageData retry:(BOOL)retry
{
    AFHTTPClient *myClient = [[AFHTTPClient alloc] initWithBaseUrl:myBaseURL];
    NSURLRequest *request = [myClient multipartFormRequestWithMethod:@"POST"                                                                                                    
                                                                path:myPath
                                                          parameters:myParametersDictionary
                                           constructingBodyWithBlock:^(id <AFMultipartFormData> formData){
                                               [formData appendPartWithFileData:imageData 
                                                                           name:myImageName 
                                                                       fileName:myFileName 
                                                                       mimeType:@"image/jpg"];
                              }];
    AFHTTPRequestOperation *operation = [myClient HTTPRequestOperationWithRequest:request
                                                                          success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                                                                // do success stuff
                                                                                  }
                                                                          failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                                                               NSLog("%@", error);
                                                                               if (retry) {
                                                                                   [self uploadImage:imageData
                                                                                               retry:NO];
                                                                               }
                                           }];
    [myClient enqueueHTTPRequestOperation:operation];
}

当然第一次你会用retry:YES

于 2013-05-22T20:59:08.207 回答