我必須下載一個文件或管理的錯誤在某些情況下(JSON)AFNetworking +下載文件或JSON響應+ setDownloadProgressBlock
下載工作很細的問題。問題是當用戶請求下載時,服務器可以在少數情況下發送404或200(帶有JSON)。
如何處理在這種情況下,JSON?當我們派遣我們不知道,如果我們將在收到JSON錯誤(200個狀態或404)或壓縮文件的請求......
我不知道怎麼responseObject能幫助我。
這裏是我的代碼:
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:zipPath append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"Successfully downloaded zip file to %@", zipPath);
// is-it impossible to handle a JSON response here ?
// responseObject can help me ? don't think !
// do what I have to do after the download is complete
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
// 404
// is-it impossible to handle a JSON response here ?
if ([error code] == -1011)
{
}
}];
[operation setDownloadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
float progress = ((float)((int)totalBytesWritten)/(float)((int)totalBytesExpectedToWrite));
self.progressView.progress = progress;
}];
[operation start];
是否我需要做一個AFJSOnRequestOperation?但在這種情況下,如何接收不是JSON的下載文件? 感謝您的幫助。
編輯:正如我在評論wroted,我可以得到,趕上responseData在成功塊只有當我發表意見行:
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:zipPath append:NO];
這是合乎邏輯的,我想響應進入的OutputStream,而不是成功塊。有沒有解決方案?
你好phix23。問題是operation.responseData當我得到JSON響應而不是文件時似乎爲零。和id jsonObject = AFJSONDecode(operation.responseData,NULL);不工作(功能AFJSONDecode的inplicit聲明是在C99無效) –
你需要使用AFHTTPRequestOperation,不AFJSONRequestOperation。對於AFJSONDecode,您必須導入AFJSONUtilities.h – Felix
更多信息。我可以得到200狀態碼,並且應用程序/ JSON mineType ......但responseData爲空。或者可以在我的瀏覽器中看到JSON。 –