Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AFX2B;下载文件或JSON响应+;setDownloadProgressBlock_Json_File_Download_Afnetworking - Fatal编程技术网

AFX2B;下载文件或JSON响应+;setDownloadProgressBlock

AFX2B;下载文件或JSON响应+;setDownloadProgressBlock,json,file,download,afnetworking,Json,File,Download,Afnetworking,在某些情况下,我下载文件或管理错误时遇到问题(JSON) 下载效果非常好。问题是,在少数情况下,当用户请求下载时,服务器可以发送404或200(带有JSON) 在这种情况下如何处理JSON?当我们发送请求时,我们不知道是否会收到JSON错误(状态为200或404)或压缩文件 我不知道responseObject能帮我什么忙 这是我的密码: AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWith

在某些情况下,我下载文件或管理错误时遇到问题(JSON)

下载效果非常好。问题是,在少数情况下,当用户请求下载时,服务器可以发送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的下载文件? 谢谢你的帮助

编辑:正如我在评论中所写的那样,只有在评论行时,我才能在成功块中获取并捕获responseData:

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

这是合乎逻辑的,我认为响应进入outputStream而不是success块。有解决方案吗?

服务器应该在标题
内容类型
中发出MIME类型的信号。 如果没有,您需要自己检测数据类型

此行确保在成功块中获得状态代码为200-499的所有响应:

[AFHTTPRequestOperation addAcceptableStatusCodes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(300, 200)]];
在成功块中,您需要确定响应的类型:

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

您还可以事先检查
操作.response.statusCode
操作.response.MIMEType

我尝试过AFJSOnRequestOperation,在.m中,我只需将输出流放在init下面:

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

requestOperation.outputStream = [NSOutputStream outputStreamToFileAtPath:[FullyLoaded filePathForResourceAtURL:urlRequest.URL.absoluteString] append:NO];
同样的情况也发生了,responseData是nil,除非注释outputStream行,所以它不是HTTPRequest或JSOnRequest问题。我使用的方法是从responseData对对象进行JSON排序并将其写入文件,而不是直接输出请求流

    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:@“应用程序/八位字节流”];:根据您的期望有所不同

Hello phix23。问题是operation.responseData在我得到JSON响应而不是文件时似乎为零。id jsonObject=AFJSONDecode(operation.responseData,NULL);不起作用(函数AFJSONDecode的inplicit声明在C99中无效)您需要使用AFHTTPRequestOperation,而不是AFJSONRequestOperation。对于AFJSONDecode,必须导入AFJSONUtilities.hMore信息。我可以得到200个状态码和application/json mineType。。。但是responseData是空的。或者可以在我的浏览器中看到JSON。确定AFJSONDecode。。。但响应数据为零:(我不知道为什么,数据在我的浏览器中…(我使用的是AFHTTPRequestOperation而不是你说的AFJSONRequestOperation)。在这种情况下,你必须打开写入的文件并读取第一个字节来检测它是JSON还是zip。如果你找到JSON格式(从
[
{
开始)阅读整个文件。当使用outputStream时,我发现了同样的问题,JSON文件被忽略。有人说outputStream路径将使响应数据为零是有意义的,因为在这种情况下,开发人员需要的是一个文件,而不是数据(已经存储在文件中)。但在某些情况下,我们总是需要这两个文件。。。