ASIHTTPRequest框架使用总结系列之阿堂教程4(下载数据)
从本篇开始,阿堂准备进一步介绍ASIHTTPRequest框架下载数据和上传数据的实际应用。
#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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。