0

ファイルのダウンロードやエラーの管理に問題がある場合があります(JSON)

ダウンロードは非常にうまく機能します。問題は、ユーザーがダウンロードを要求したときに、サーバーが404または200(JSONを使用)を送信できる場合があることです。

この場合のJSONの処理方法は?リクエストを送信すると、JSONエラー(ステータスが200または404)を受信するのか、zipファイルを受信するのかわかりません...

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ではないダウンロードされたファイルを受け取る方法は?助けてくれてありがとう。

編集:コメントに書いたように、次の行にコメントした場合にのみ、成功ブロックでresponseDataを取得してキャッチできます。

 operation.outputStream = [NSOutputStream outputStreamToFileAtPath:zipPath append:NO];

それは論理的です、私は応答が成功ブロックの代わりにoutputStreamに入ると思います。そのための解決策はありますか?

4

3 に答える 3

1

サーバーはヘッダーで MIME タイプを通知する必要がありますContent-Type。そうでない場合は、データ型を自分で検出する必要があります。

この行により、成功ブロックでステータス コード 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.statusCodeたり することもできます。operation.response.MIMEType

于 2012-06-19T10:44:29.863 に答える
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:@"application/octet-stream"]; : 期待する内容によって異なる場合があります

于 2015-01-21T05:03:27.653 に答える
0

私はAFJSOnRequestOperationで試しました.mで、initの下にoutputStreamを配置するだけです:

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

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

同様に、outputStream 行をコメントしない限り、responseData は nil であるため、HTTPRequest または JSOnRequest の問題ではありません。私が回避するために使用する方法は、リクエストを直接出力ストリームするのではなく、responseData からオブジェクトを JSON シーケンスし、それをファイルに書き込むことです。

于 2012-09-24T05:36:54.200 に答える