2012-06-19 163 views
0

我必須下載一個文件或管理的錯誤在某些情況下(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,而不是成功塊。有沒有解決方案?

回答

1

服務器應在標題Content-Type信號的MIME類型。 如果不是,你需要自己檢測數據類型。

這條線,確保獲得與的StatusCode 200-499所有響應的成功塊:

[AFHTTPRequestOperation addAcceptableStatusCodes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(300, 200)]]; 

在成功塊,你需要確定響應類型:

id jsonObject = AFJSONDecode(operation.responseData, NULL); 
if (jsonObject) { 
    // handle JSON 
} else { 
    // handle zip file 
} 

你可以事先也檢查operation.response.statusCodeoperation.response.MIMEType

+0

你好phix23。問題是operation.responseData當我得到JSON響應而不是文件時似乎爲零。和id jsonObject = AFJSONDecode(operation.responseData,NULL);不工作(功能AFJSONDecode的inplicit聲明是在C99無效) –

+0

你需要使用AFHTTPRequestOperation,不AFJSONRequestOperation。對於AFJSONDecode,您必須導入AFJSONUtilities.h – Felix

+0

更多信息。我可以得到200狀態碼,並且應用程序/ JSON mineType ......但responseData爲空。或者可以在我的瀏覽器中看到JSON。 –

0

我試着AFJSOnRequestOperation,在.M我乾脆把OutputStream的初始化之下:

AFJSONRequestOperation *requestOperation = [[[self alloc] initWithRequest:urlRequest] autorelease]; 

requestOperation.outputStream = [NSOutputStream outputStreamToFileAtPath:[FullyLoaded filePathForResourceAtURL:urlRequest.URL.absoluteString] append:NO]; 

而同樣的情況發生,responseData是零,除非評論OutputStream的線,所以它不是的HTTPRequest或JSOnRequest問題。我用來解決的方法是JSON對responseData中的對象進行排序並將其寫入文件,而不是直接輸出請求。

+1

我在使用outputStream時發現了同樣的問題JSON文件被忽略 –

+0

有人說,outputStream路徑將無響應數據,因爲在這種情況下,開發人員需要的是一個文件,而不是數據(已存儲在文件中)。但總有一些情況下,我們需要兩個... – BabyPanda

0
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 

    manager.responseSerializer = [AFCompoundResponseSerializer serializer]; 

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/octet-stream"]; 

    AFHTTPRequestOperation *operation = [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 

    if (responseObject) { 

    } 
    else{ 

     } 

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


    }]; 

[operation start]; 

// manager.responseSerializer.acceptableContentTypes = [NSSet中setWithObject:@ 「應用程序/八位字節流」]; :可以根據你的期望而變化