NSURLSessionDownloadTaskDelegate JSON响应 [英] NSURLSessionDownloadTaskDelegate JSON response

查看:102
本文介绍了NSURLSessionDownloadTaskDelegate JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个后台NSURLSession会话,我试图找出一种方法来获取NSURLDownloadTaskDelegate回调中的一个JSON响应。我已将会话配置为接受JSON响应。

I am running a background NSURLSession session and i am trying to figure out a way to get the JSON response out of one of the NSURLDownloadTaskDelegate callbacks. I have configured my session to accept JSON responses.

NSURLSessionConfiguration *backgroundSession = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.Att.Locker.BackgroundUpload"];
backgroundSession.HTTPAdditionalHeaders = @{ @"Accept":@"application/json"};
session = [NSURLSession sessionWithConfiguration:backgroundSession delegate:uploader delegateQueue:nil];

我可以使用以下回调轻松解析NSURLSessionDownloadTasks的JSON响应。它以NSURL的形式将JSON响应写入沙箱。

I can easily parse JSON response for NSURLSessionDownloadTasks using the following callback. It writes the JSON response onto the sandbox in the form of NSURL.

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location  {

  //Reading the Json response from the sandbox using the NSURL parameter
}

我的问题是如果我遇到错误,上面的回调没有被调用,它似乎只有在成功下载的情况下被调用。由于我使用后台会话,因此无法使用任何NSURLSessionDataDelegate回调。我只能使用NSURLSessionDownloadTaskDelegate和NSURLSessionTaskDelegate,而我可以使用以下回调获取任务响应。我在响应中没有看到JSON。

My problem is if i encounter an error the callback above is not called, it seems to only get invoked in case of a successful download. Since i am using a background session i cannot use any of the NSURLSessionDataDelegate callbacks. I can only use the NSURLSessionDownloadTaskDelegate and NSURLSessionTaskDelegate and while i can get the task response using the following callback. I don't see the JSON in the response.

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {


NSHTTPURLResponse *response = (NSHTTPURLResponse *)downloadTask.response;
NSDictionary *httpResponse = [response allHeaderFields];

NSLog(@"Response Header Fields:%@",[httpResponse allKeys]);
}

NSURLConnection 有一个 didReceiveData 参数,它为我们提供了一个 NSData 对象,我们可以用它来获取JSON响应。我没有在 NSURLSession 的委托回调中看到,除了 NSURLDataTask ,但是我们无法在后台使用数据任务,那么我们如何才能获得JSON响应呢?任何帮助表示赞赏

NSURLConnection has a didReceiveData parameter which gives us an NSData object which we can use to get the JSON response. I don't see that in the delegate callbacks for NSURLSession except for NSURLDataTask but we cant use data tasks in the background so how are we supposed to get the JSON response out ? Any help is appreciated

编辑:

我在后台运行应用程序时遇到此问题(主要是当它被踢出记忆而不是暂停)。我已经在appDelegate中实现了回调,并且我能够重新关联session.I认为didFinishDownloadingToURL仅在成功完成任务时调用,但是当任务失败时,没有保证将被调用但另一方面每次出现故障时都会调用didCompleteWithError

I usually experience this issue while i am running the app in the background (mostly when it is kicked out memory and not just suspended). I have implemented the callbacks in the appDelegate and i am able to re associate with the session.I think didFinishDownloadingToURL is only invoked in case of successful completion of a task but when a task fails there is no guarantee its going to be called but on the other hand didCompleteWithError gets called every time there is a failure

推荐答案

使用下载任务,您可以如你所述,使用 didFinishDownloadingToURL 获取数据。

Using a download task, you can get the data using the didFinishDownloadingToURL as you stated.

所有NSURLSession任务也都有此委托。如果你进入这里并且错误不是零,那么你就错了。它不需要完成进入这里。如果它确实在这里出现错误,那么将不会调用委托 didFinishDownloadingToURL

All NSURLSession tasks have this delegate as well. If you get in here and error is not nil, then walah you have an error. It does not need to complete to get in here. If it does get in here with an error, then the delegate didFinishDownloadingToURL will not be called.

如果有没有错误,并且所有数据下载都将被调用。

If there is no error, and all of your data downloads, than both delegates will be called.

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    NSLog(@"didCompleteWithError");
}

编辑:

因此必须设置正确的方法,因为必须有一种方法来获取数据。

So something has to bet not setup correctly as there has to be a way to get the data.

您是否正在实施 application:handleEventsForBackgroundURLSession: completionHandler :在您的AppDelegate中,它会将您的应用程序挂钩到完成处理程序以获取委托调用?

Are you implementing application:handleEventsForBackgroundURLSession:completionHandler: in your AppDelegate which will hook your app back up to the completion handler to get the delegate calls?

我强烈建议您观看2013 WWDC会话# 705,基础网络新事物。后台会话开始大约32分钟,代码演示开始于37:50

I highly recommend watching the 2013 WWDC session #705, "Whats New in Foundation Networking". Background session talk begins at around 32 minutes, and the code demo begins around 37:50

这篇关于NSURLSessionDownloadTaskDelegate JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆