- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从网站获取数据以将其显示在表格 View 中
我的代码:
-(void)loadTutorials {
NSURL *url = [NSURL URLWithString:[@"http://www.example.com/search?q=" stringByAppendingString:self.searchString]];
NSURLRequest *UrlString = [[NSURLRequest alloc] initWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:UrlString
delegate:self];
[connection start];
NSLog(@"Started");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:data];
NSString *tutorialsXpathQueryString = @"//div[@id='header']/div[@class='window']/div[@class='item']/div[@class='title']/a";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
NSMutableArray *newTutorials = [[NSMutableArray alloc] init];
for (TFHppleElement *element in tutorialsNodes) {
Data *tutorial = [[Data alloc] initWithTitle: [[element firstChild] content]
Url: [@"http://www.example.com" stringByAppendingString: [element objectForKey:@"href"]]
];
[newTutorials addObject:tutorial];
}
_objects = newTutorials;
[self.tableView reloadData];
}
但是数据没有显示出来,是不是数据没有加载完?我让它在没有 NSURLConnection 的情况下工作,但是它会停止程序直到收到数据
最佳答案
根据 NSURLConnectionDataDelegate
connection:didReceiveData:
以增量方式调用。
The newly available data. The delegate should concatenate the contents of each data object delivered to build up the complete data for a URL load.
所以这意味着您应该在此方法中附加新数据。
然后,在
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
您应该操纵您的数据。
所以,例如
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// Create space for containing incoming data
// This method may be called more than once if you're getting a multi-part mime
// message and will be called once there's enough date to create the response object
// Hence do a check if _responseData already there
_responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data
[_responseData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Parse the stuff in your instance variable now
}
显然,您还应该实现负责错误处理的委托(delegate)。
下面是一个简单的注释。如果数据太大并且你需要做一些计算(例如解析),你可以阻止 UI。所以,你可以在不同的线程中移动解析(GCD 是你的 friend )。完成后,您可以在主线程中重新加载表格。
也可以在这里查看更多信息:NSURLConnectionDataDelegate order of functions .
关于ios - NSURLConnection didReceiveData 未加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20923142/
现在我正在我的 iOS 应用程序中进行 POST,并且在 POST 中我有这个 NSURLConnection *connection = [[NSURLConnection alloc] initW
我正在开发从Web服务器获取数据的ios应用程序。我希望其他所有内容都等待,直到该类的处理程序之一被调用并完成。我知道可以通过使用调度/线程,但是我只是不知道如何。 -(void)callWebSer
我有一个类用于从我的服务器获取数据。从我的服务器返回的数据是 JSON。由于某种原因, didReceiveData 根本不会运行。我已将 NSLogs 放入其中进行测试,但它什么也没做? 这是我的代
我尝试监听来自 dweet.io 的数据服务。我用listen实时获取数据的方法。它使用分块的 HTTP 响应。 我为此创建了简单的网络管理器 import Foundation class Netw
我对 swift 很陌生,我有以下代码 var data : AnyObject let dict = jsonObject as NSDictionary do {
我正在尝试从网站获取数据以将其显示在表格 View 中 我的代码: -(void)loadTutorials { NSURL *url = [NSURL URLWithString:[@"ht
我想使用 ASIHTTPRequest 下载文件,我希望它的行为类似于常规的直接文件下载,除了我想在数据传入时对其进行加密。 因为我需要自定义数据处理,所以我需要让我的委托(delegate)实现请求
我有一个 GET 服务调用,我必须在收到响应时执行某些操作。 AFNetworking 中的委托(delegate)方法实际上是 block ,据我所知,仅在请求完成或完全失败时通知。 稍微深入了解一
我想在这里获得一些设计/代码建议。 我有一个应用程序需要调用网络服务的 3 个 API。这些发生在不同的 View 中,但我需要来自这 3 个 api 的所有信息都在一个对象中。因此,我将一个对象从
我正在将 AFNetworking2 用于我的 iOS 项目,该项目需要大量图像加载。 下载一定数量的图片后,所有发出的请求都会在操作队列中排队,永远不会出去。 我发现那是因为在 AFURLConne
我正在从这个 React Native 模块中获取一些代码 react-native-file-upload .我正在尝试将其从使用 NSURLConnection(已弃用)更新为使用 NSURLSe
我正在使用 GameKit 开发多人 Swift (XCode 7.3/iOS 9.2) 游戏。 我使用自定义结构作为通过 GameKit 发送的信息。该结构的副本如下。 frustnessNetwo
我有一个问题。我已经阅读了很多关于这个问题的关于 stackoverflow 的主题,但没有任何帮助。我正在尝试通过 cometd 连接实现客户端-服务器通信。我有一个负责发送消息的实例和一个负责接收
我通读了大量的消息,一遍又一遍地说同样的话:当你使用 NSURLConnection 时,委托(delegate)方法不会被调用。我知道 Apple 的文档不完整并且引用了已弃用的方法,这很遗憾,但我
我遇到了 didReceiveData 和 didCompleteWithError 没有被调用的问题。这是我的代码: class LoginViewController: UIViewControl
我真的很难弄清楚为什么我的网络服务调用充满了垃圾数据。 我有一个调用 Web 服务的 UITableViewController,它还充当 NSURLConnectionDelegate。 这里是感兴
我在植入简单的 NSURLConnection 时遇到了这个奇怪的问题... 调用了 didReceiveData 方法,我很乐意尝试附加接收数据,但是... nada! 肯定有一些数据(如长度所示,
我需要一些有关 NSURLConnectionDelegate 方法的帮助。 - (void)startDownload { NSString *URLString = [NSString strin
我正在启动一个请求,该请求返回带有重定向的 HTML。为什么 didReceiveData 函数没有被调用第二次?我正在尝试下载一个 JSON 文件。 (使用 iOS6) - (void) t
我想从某个服务器下载一些文件。下载文件的功能在 Thread 中运行。所以,你可以在这里看到它: NSURL *url = [NSURL URLWithString:fileDataBaseU
我是一名优秀的程序员,十分优秀!