iPhone开发教程之开发基于IBMLotusConnections2.5的社交网络iPhone应用程序2课件.pptx
文本预览下载声明
iPhone开发教程之开发基于 IBM Lotus Connections 2.5 的社交网络 iPhone 应用程序(2)清单 2. 处理 NSURLConnection 响应清单 3. 获取响应数据清单 4. 创建解析器并发送数据以便解析清单 5. 调用 NSXMLParser清单 6. 检查 elementNames清单 7. 保存标题元素的文本值清单 2. 处理 NSURLConnection 响应////////////////////////////////////////////////////////////////////////////////////- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSHTTPURLResponse*)response { //response saved so that status Codes can be checked later _response = [response retain]; NSDictionary* headers = [response allHeaderFields]; int contentLength = [[headers objectForKey:@Content-Length] intValue];//append the responseData used in connectionDidFinishLoading: _responseData = [[NSMutableData alloc] initWithCapacity:contentLength];}?当从连接接收到数据后,将所有数据添加到 responseData 对象中。此方法有可能不止一次被调用;这就是我们添加数据而不是设置数据的原因。见清单 3。清单 3. 获取响应数据////////////////////////////////////////////////////////////////////////////////////- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data { [_responseData appendData:data]; }连接完成加载后,调用委托对象方法 connectionDidFinishLoading。现在已从请求返回所有数据,并将数据发送给了解析器,以便能获得更新提要中的博客条目标题。 由于现在已经从服务器接收完数据,请求就完成了,可以关闭网络活动指示器了,如清单 4 所示。清单 4. 创建解析器并发送数据以便解析////////////////////////////////////////////////////////////////////////////////////- (void)connectionDidFinishLoading:(NSURLConnection *)connection {? [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; AtomParser *parser = [[AtomParser alloc] initWithData:_responseData]; [parser parse];_items = parser.items; [_tableView reloadData];}最后,创建一个解析 Atom 提要的类。首先在 XCode 项目中创建一个 Objective-C 类。将 Atom 提要解析器命名为 AtomParser.m 和 AtomParser.h。然后进行修改,让 AtomParser.h 成为 NSXMLParser 的子类,并实现 NSXMLParserDelegate 协议。这在 AtomParser.h 中进行,完成后像这样: @interface AtomParser : NSXMLParser NSXMLParserDelegate 现在已创建了 AtomParser 类,现在需要创建清单 5 中调用的解析方法,该清单下显示了 AtomParser.m 中的解析方法所包含的内容。清单 5. 调用 NSXMLParser- (BOOL)parse { _items = [[NSMutableArray alloc] init]; self.delegate = self; BOOL result = [super parse]; return result;}
显示全部