2014-02-22 95 views
7


如何獲取AFHTTPRequest的進度?我試過在網上搜索。
我使用:AFNetworking 2.0 HTTP POST進度

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    NSDictionary *params = @{@"gameId" : datas[0], @"p1": datas[1], @"p2":datas[2], @"turn":datas[3] }; 
    manager.requestSerializer = [AFHTTPRequestSerializer serializer]; 
    manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 
    [manager POST:@"http://localhost/thepath/isprivate/thefile.php" 
     parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { 

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

有沒有想,一個屬性或方法我可以用它來訪問的AFNetworking 2.0 HTTP POST的進展如何?

+2

嗨,你可以檢查這個答案http://stackoverflow.com/a/20543730/1228669 – prasad

回答

1

您可以使用下面的方法

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request 
             fromData:(NSData *)bodyData 
             progress:(NSProgress * __autoreleasing *)progress 
           completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler 
AFHTTPSessionManager

UPDATE:

通常你會更喜歡使用KVO得到上傳的值。因此,像以下應使用:

static void * kDGProgressChanged = &kDGProgressChanged; 

... 

[progress addObserver:self 
      forKeyPath:@"fractionCompleted" 
       options:NSKeyValueObservingOptionNew 
       context:kDGProgressChanged]; 

... 

- (void)observeValueForKeyPath:(NSString *)keyPath 
         ofObject:(id)object 
         change:(__unused NSDictionary *)change 
         context:(void *)context 
{ 
    if (kDGProgressChanged == context) { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self updateProgressInfo]; 
     }); 
    } 
} 
+0

我用這個方法,但不叫進步。雖然我使用KVO進行監控,但上傳操作已成功完成,文件已上傳但未跟蹤進度。怎麼了? – lenhhoxung

+0

@lenhhoxung請檢查我的更新的答案。 – Avt

19

試試這個:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
NSDictionary *params = @{@"gameId" : datas[0], @"p1": datas[1], @"p2":datas[2], @"turn":datas[3] }; 
manager.requestSerializer = [AFHTTPRequestSerializer serializer]; 
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:@"http://localhost/thepath/isprivate/thefile.php" parameters:params error:&error]; 

AFHTTPRequestOperation *requestOperation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"operation success: %@\n %@", operation, responseObject); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", error); 
}]; 

[requestOperation setUploadProgressBlock:^(NSUInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) { 
    double percentDone = (double)totalBytesWritten/(double)totalBytesExpectedToWrite; 
    NSLog(@"progress updated(percentDone) : %f", percentDone); 
}]; 
[requestOperation start]; 

希望這有助於。

+1

謝謝,但Avt最早回覆。 +1給你,但謝謝。 – DHShah01

10

的捷徑的方式來實現,這是

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    AFHTTPRequestOperation *requestOperation = [manager POST:@"/service/user/authenticate" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     //Your Business Operation. 
     NSLog(@"operation success: %@\n %@", operation, responseObject); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", error); 
    }]; 
//Here is the upload progress 
[requestOperation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { 
      double percentDone = (double)totalBytesWritten/(double)totalBytesExpectedToWrite; 
      //Upload Progress bar here 
      NSLog(@"progress updated(percentDone) : %f", percentDone); 
     }]; 
+0

看起來您正在設置成功塊內的進度塊。這真的有用嗎?我認爲這將是無用的,你基本上設置了成功完成後的進度塊。 –

+0

@SterlingChristensen感謝您的通知。該代碼已被適當編輯。感謝 –

+0

這是AFN 2.0更短的方式,謝謝 – Kuba