2013-01-19 164 views
3

我正在使用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]; 

回答

3

我使用此代碼對一些應用程序的和還沒有出現過任何問題與它(注意我用的是AFJSONRequestOperation和AFRestClient)

設置請求

// I have a shared singleton in a separate file APIClient.h 
AFRESTClient *httpClient = [APIClient sharedClient]; 

NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:method parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) 
    {[formData appendPartWithFileData:imageData 
       name:@"image" 
       fileName:@"image.png" 
       mimeType:@"image/png"]; 
    }]; 

則操作,

AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request]; 

我的進步塊

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { 

    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite); 
    CGFloat progress = ((CGFloat)totalBytesWritten)/totalBytesExpectedToWrite * 100; 
    [SVProgressHUD showProgress:50 status:[NSString stringWithFormat:@"%0.1f %%", progress]]; 

}];  

成功塊(我的API返回的統計-ok或失敗,則

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id response) { 
    NSLog(@"response string: %@", operation.responseString); 
    NSString *resultStat = [response objectForKey:@"stat"]; 

    if ([resultStat isEqualToString:@"ok"]) { 
     //success 
     // do something with your data that is returned from the API 
    } else { 
     //error 
     NSLog(@"Data Error: %@", response); 
    } 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", [error localizedDescription]); 
}]; 

[httpClient enqueueHTTPRequestOperation:operation]; 
0

不能看到你的代碼的任何問題,幾種可能性:

嘗試設置短超時間隔並檢查請求是否完成 [request setTimeoutInterval:5];

也嘗試註釋掉節流行,看看它是否改變任何東西

+0

註釋掉節流的數據(同Flickr的API),設置超時非常低。還遵循AFNetworking人員的一些提示來保留httpclient,並使用[self.httpClient enqueueHTTPRequestOperation:operation]而不是[operation start]。仍然不會通過完成塊。 我確實發現它確實有效,但我不會將filedata添加到請求中。 –

+0

非常好,可能是服務器端的東西,想要重新檢查/發佈您的Web服務腳本? –

相關問題