- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSDictionary *dict =
[NSJSONSerializationJSONObjectWithData:_dataoptions:NSJSONReadingMutableContainerserror:nil]; //涨幅榜
NSMutableArray *zfbArray = [NSMutableArrayarray];
for (int i = 0; i < [dict[@\][@\] count]; i ++) { ZMPStock *stock = [[ZMPStockalloc] init];
stock.stockName = dict[@\][@\][i][@\]; stock.stockCode = dict[@\][@\][i][@\]; stock.stockIndex = dict[@\][@\][i][@\];
stock.stockCurrentPrice = dict[@\][@\][i][@\]; [zfbArray addObject:stock];
}
//跌幅榜
NSMutableArray *dfbArray = [NSMutableArrayarray];
for (int i = 0; i < [dict[@\][@\] count]; i ++) { ZMPStock *stock = [[ZMPStockalloc] init];
stock.stockName = dict[@\][@\][i][@\]; stock.stockCode = dict[@\][@\][i][@\]; stock.stockIndex = dict[@\][@\][i][@\];
stock.stockCurrentPrice = dict[@\][@\][i][@\]; [dfbArray addObject:stock]; }
//换手率榜
NSMutableArray *hslbArray = [NSMutableArrayarray];
for (int i = 0; i < [dict[@\][@\] count]; i ++) { ZMPStock *stock = [[ZMPStockalloc] init];
stock.stockName = dict[@\][@\][i][@\]; stock.stockCode = dict[@\][@\][i][@\]; //stock.stockIndex = dict[@\
stock.stockCurrentPrice = dict[@\][@\][i][@\]; stock.turnOverRate = dict[@\][@\][i][@\]; [hslbArray addObject:stock]; }
//振幅榜
NSMutableArray *zfBArray = [NSMutableArrayarray];
for (int i = 0; i < [dict[@\][@\] count]; i ++) { ZMPStock *stock = [[ZMPStockalloc] init];
stock.stockName = dict[@\][@\][i][@\]; stock.stockCode = dict[@\][@\][i][@\]; //stock.stockIndex = dict[@\
stock.stockCurrentPrice = dict[@\][@\][i][@\]; stock.amplitude = dict[@\][@\][i][@\]; [zfBArray addObject:stock]; }
[_dataArrayaddObject:zfbArray]; [_dataArrayaddObject:dfbArray]; [_dataArrayaddObject:hslbArray]; [_dataArrayaddObject:zfBArray]; [_tableViewreloadData]; }}
(3)返回类型为xml格式
在爱炒股软件中,大多数的股讯信息都是xml格式,以股讯板块中得要闻为例,请求连接为:http://news.10jqka.com.cn/headline_mlist/1_0_1_1/,返回数据如下:
21
关键代码如下:
22
- (void)loadData:(NSString *)url{
//所有的请求都需要AFHTTPRequestOperationManager发送请求
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager]; //返回数据类型为二进制
manager.responseSerializer = [AFHTTPResponseSerializerserializer];
//get请求
[manager GET:url parameters:nilsuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[self.headerendRefreshing]; [self.footerendRefreshing]; if (flag) {
[_dataArrayremoveAllObjects]; } GDataXMLDocument *xml = [[GDataXMLDocumentalloc] initWithData:responseObject options:0error:nil];
NSArray *news = [xml nodesForXPath:@\error:nil]; //NSLog(@\
int i = 0;
for (GDataXMLElement *element in news) {
NSArray *title = [element nodesForXPath:@\error:nil]; NSArray *time = [element nodesForXPath:@\error:nil]; NSArray *imgurl = [element nodesForXPath:@\error:nil];
NSArray *url = [element nodesForXPath:@\error:nil];
ZMPImportantNewsTableViewCellNews *news = [[ZMPImportantNewsTableViewCellNewsalloc] init]; news.newsImageUrl = [imgurl[0] stringValue]; news.newsTitle = [title[0] stringValue]; news.newsTime = [time[0] stringValue]; news.newsUrl = [url[0] stringValue];
if (i >0) {
[_dataArrayaddObject:news]; } i ++; }
[_importantNewsTableViewreloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@\请求失败%@\,error); }]; }
23
6.2数据的请求及刷新
6.2.1 数据的请求
在iStock应用中,数据的请求操作是通过GET请求完成,即通过URL的方式请求获取相关数据。
为了用户有着良好的体验效果,主要通过异步请求的方式。关键代码:
NSString *urlStr = [NSStringstringWithFormat:@\,tempStr]; //请求对象
NSURLRequest *request = [NSURLRequestrequestWithURL:[NSURLURLWithString:urlStr]]; [NSURLConnectionconnectionWithRequest:request delegate:self];
通过NSURLConnection类来实现数据的异步加载操作,获取的数据需要在代理的方法中进行实现。
另外,为了增强用户的体验性,需要对加载做一个时间设定,通过request的一个属性timeoutInterval来实现,例如:
request.timeoutInterval = 15.0;//请求时间限制
在设置后,如果请求数据超过该设定时间,程序就会进入到connection:(NSURLConnection *)connection didFailWithError:(NSError *)error这个方法中。关键代码:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ _data.length = 0; }
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [_dataappendData:data]; }
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ [selfdataParaing:_data]; }
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ [KVNProgressshowErrorWithStatus:@\请求数据失败\]; }
6.2.2 NSTimer定时刷新
NSTimer主要用在自选股数据的刷新,通过设置刷新频率来控制其请求数据的次数。考虑到在2G
网的状态下,网络请求数据相对较慢,会对用户的体验造成一定的影响,所以默认刷新频率设置为不刷新。
当设置其刷新频率为5s、15s、30s、60s时,通过全局变量refershFrequencyWithOutWifi和
refershFrequencyWithWifi进行控制。
24
/**
2G/3G/4G网下得刷新频率 */
@property (nonatomic,assign) int refershFrequencyWithOutWifi; /**
在WiFi下得刷新频率 */
@property (nonatomic,assign) int refershFrequencyWithWifi;
当改变其刷新频率时,不能只是简单地进行赋值操作,而是首先判断是否有计时器timer在线程
中,如果在那么需要将其置为nil后,才能进行赋值操作。 关键代码:
//先用isValid先判断是否还在线程中 if ([timerisValid] == YES) { [timerinvalidate]; timer = nil; }
//刷新频率等于0,表示不刷新
if (app.refershFrequencyWithOutWifi != 0) { //定时刷新 timer =
[NSTimerscheduledTimerWithTimeInterval:app.refershFrequencyWithOutWifitarget:selfselector:@selector(loadData) userInfo:nilrepeats:YES]; }else{
[timerinvalidate]; timer = nil;
NSLog(@\停止刷新\); }
当然,如果当前页面消失后,NSTimer依然存留在线程中,这固然是不好的,所以当页面消失时
将NSTimer进行暂停或者关闭,当页面即将出现时,再将其开启,这也对线程的一种优化,提升应用的运行效率。 关键代码:
- (void)viewDidDisappear:(BOOL)animated{ [superviewDidDisappear:animated];
[timersetFireDate:[NSDatedistantFuture]]; }
- (void)viewWillAppear:(BOOL)animated{ [superviewWillAppear:animated];
[timersetFireDate:[NSDatedistantPast]]; [_tableViewreloadData]; }
25