从本篇开始,阿堂准备进一步介绍ASIHTTPRequest框架下载数据和上传数据的实际应用。
为了实现多线程并发请求网络能力,ASIHTTPRequest被设计成 NSOperation的子类。ASINetworkQueue被设计成NSOpertaionQueue的子类。如果NSOpertaionQueue是线程管理器,NSOperation就相当于一个线程。它们被添加到NSOperationQueue队列中有序执行。ASINetworkQueue 和ASIHTTPRequest也有同样的概念,只是ASINetworkQueue线程管理器提供更多的与网络相关的服务,如获得下载进度等。所以,下面阿堂就主要介绍ASINetworkQueue管理请求队列的demo了。
下面,请随阿堂看一个例子介绍队列的用法。当我们点击go按钮时,从服务器端同时下载两张图片并将它们显示在界面中。
其中两个文件的完整源代码如下
ViewController.h, ViewController.m
#import
#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"
#import "NSNumber+Message.h"
#import "NSString+URLEncoding.h"
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@property (strong) ASINetworkQueue *networkQueue;
- (IBAction)onClick:(id)sender;
@end
---------------------------------------------------------
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)onClick:(id)sender {
if (!_networkQueue) {
_networkQueue = [[ASINetworkQueue alloc] init];
}
// 停止以前的队列
[_networkQueue cancelAllOperations];
// 创建ASI队列
[_networkQueue setDelegate:self];
[_networkQueue setRequestDidFinishSelector:@selector(requestFinished:)];
[_networkQueue setRequestDidFailSelector:@selector(requestFailed:)];
[_networkQueue setQueueDidFinishSelector:@selector(queueFinished:)];
for (int i=1; i<<span style="color: #272ad8">3; i++) {
NSString *strURL = [[NSString alloc] initWithFormat:@"http://www.crazyit.com/service/download.php?email=%@&FileName=test%i.jpg",@"[email protected]",i];
NSURL *url = [NSURL URLWithString:[strURL URLEncodedString]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
request.tag = i;
[_networkQueue addOperation:request];
}
[_networkQueue go];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSData *data = [request responseData];
// NSLog(@"data = %@",data);
NSError *eror;
NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:dataoptions:NSJSONReadingAllowFragments error:&eror];
//如果服务器是返回的图片过来,那是通过数据io流过来的,这里的 resDict值一定会为null, 因为 data不是json格式,所以NSJSONSerialization处理后是不能正解成json格式的。
NSLog(@"resDict = %@",resDict);
if (!resDict) {
UIImage *img = [UIImage imageWithData:data];
if (request.tag ==1) {
_imageView1.image = img;
} else {
_imageView2.image = img;
}
} else {
//如果服务器是返回的图片过来,那是通过数据io流过来的,这里的 resDict值一定会为null, 因为 data不是json格式,所以NSJSONSerialization处理后是不能正解成json格式的。
//如果是返回 json格式,一般类似于 {"ResultCode":-1} 格式,如果是图片io流返回, 一定不是json格式返回了
NSNumber *resultCodeObj = [resDict objectForKey:@"ResultCode"];
NSString *errorStr = [resultCodeObj errorMessage];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"错误信息"
message:errorStr
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alertView show];
}
if ([_networkQueue requestsCount] == 0) {
[self setNetworkQueue:nil];
}
NSLog(@"请求成功");
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"%@",[error localizedDescription]);
if ([_networkQueue requestsCount] == 0) {
[self setNetworkQueue:nil];
}
NSLog(@"请求失败");
}
- (void)queueFinished:(ASIHTTPRequest *)request
{
if ([_networkQueue requestsCount] == 0) {
[self setNetworkQueue:nil];
}
NSLog(@"队列完成");
}
@end
-------------------------
#import
@interface NSNumber (Message)
-(NSString *)errorMessage;
@end
------------------------
#import "NSNumber+Message.h"
@implementation NSNumber (Message)
-(NSString *)errorMessage
{
NSString *errorStr = @"";
switch ([self integerValue]) {
case -9:
errorStr = @"文件没有指定。";
break;
case -8:
errorStr = @"文件找不到。";
break;
case -7:
errorStr = @"没有数据。";
break;
case -6:
errorStr = @"日期没有输入。";
break;
case -5:
errorStr = @"内容没有输入。";
break;
case -4:
errorStr = @"ID没有输入。";
break;
case -3:
errorStr = @"据访问失败。";
break;
case -2:
errorStr = @"您的账号最多能插入10条数据。";
break;
case -1:
errorStr = @"用户不存在,请到http://www.crazyit.com注册。";
default:
break;
}
return errorStr;
}
@end