【问题标题】:Copying AFHTTPRequestOperation results in error "request body stream exhausted"复制 AFHTTPRequestOperation 导致错误“请求正文流已用尽”
【发布时间】:2013-05-22 19:56:57
【问题描述】:

问题

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

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

这是我的重试代码:

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:] 方法似乎提供了输入流的副本。我在那里设置了一个断点;复制或启动操作时没有调用,不知道怎么触发。

关于similar issue on the AFNetworking GitHub page 有一些讨论,但这与身份验证失败后重试有关。

其他信息

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

【问题讨论】:

  • 为什么不调用最初尝试上传的方法?
  • 此时视图已关闭,文本框已解除分配,图像已适当压缩,等等。我想我可以保留所有这些内容并从头开始重建新操作。文档说“复制机制返回一个全新的实例,这对于重试操作很有用” - 所以看起来我应该能够只复制操作并重试。

标签: ios afnetworking


【解决方案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调用它

【讨论】:

  • 这样的事情可能会奏效,但我的问题是关于 AFHTTPClient 记录的对复制 AFHTTPRequestOperation 对象以重试它的支持。不过,+1 是一个可用的解决方法。
猜你喜欢
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 2018-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多