标签:
从本篇开始,阿堂准备进一步介绍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",@"heyitang@qq.com",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
ASIHTTPRequest框架使用总结系列之阿堂教程4(下载数据)
标签:
原文地址:http://www.cnblogs.com/lisa090818/p/4249740.html