ARC下采用的第三方库网络下载&nbsp…

来源:互联网 发布:做电子相册什么软件好 编辑:程序博客网 时间:2024/06/06 09:36

需要借助第三方库AFNetworking


#import "ViewController.h"

#import "AFNetworking.h"


@interface ViewController(){

    UIImageView* _imageView;

   UIProgressView* _pv;

   AFImageRequestOperation* _operation;

}


@end


@implementationViewController


- (void)viewDidLoad

{

   [superviewDidLoad];

   //断点续传 请求队列

    

   //[self httpRequest];

   //[self jsonRequest];

   //[self imageRequest];

   //[self client];

    [self makeView];

}


- (void)makeView{

   _imageView =[[UIImageViewalloc]initWithFrame:CGRectMake(0,20, 320,240)];

    [self.view addSubview:_imageView];


   _pv= [[UIProgressView alloc] initWithFrame:CGRectMake(50,300, 220,20)];

    [self.view addSubview:_pv];

    

    NSArray* array = @[@"开始",@"暂停",@"继续"];

    for (int i = 0; i < 3; i++) {

       UIButton*button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

       button.frame= CGRectMake(20 + 100 * i, 350, 80, 20);

       [buttonsetTitle:array[i]forState:UIControlStateNormal];

       button.tag =i;

       [buttonaddTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

       [self.view addSubview:button];

    }

    

    //请求

   NSURLRequest*request = [NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://img0.bdstatic.com/img/image/shouye/mxtfboy-13712786966.jpg"]];

    

   _operation =[[AFImageRequestOperation alloc]initWithRequest:request];

   //指定下载目录

   NSString* path= [NSHomeDirectory()stringByAppendingPathComponent:@"Documents/a.jpg"];

    NSLog(@"%@", path);

    NSURL* url = [NSURL fileURLWithPath:path];

   _operation.outputStream =[NSOutputStreamoutputStreamWithURL:urlappend:NO];

    //回调

    [_operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation*operation, id responseObject){

       NSLog(@"下载完成");

    } failure:^(AFHTTPRequestOperation *operation,NSError *error) {

       NSLog(@"请求失败");

    }];

   //进度条

   __weak UIProgressView* pv = _pv;

    [_operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {

       [pv setProgress:(float)totalBytesRead/totalBytesExpectedToReadanimated:YES];

    }];


}


- (void)buttonClick:(UIButton*)button{

    //开始

    if (button.tag == 0) {

       [_operationstart];

    }

    //暂停

    if (button.tag == 1) {

       [_operationpause];

    }

    //继续

    if (button.tag == 2) {

       [_operationresume];

    }

}


- (void)client{

   

    AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://10.0.8.8/sns/"]];

   //发起一个get请求

    [client getPath:@"my/user_list.php?page=1" parameters:nil success:^(AFHTTPRequestOperation *operation,id responseObject) {

       

    } failure:^(AFHTTPRequestOperation *operation,NSError *error) {

       NSLog(@"请求失败");

    }];

   //发起一个post请求

    [client postPath:@"my/user_list.php" parameters:@{@"page":@"1"}success:^(AFHTTPRequestOperation *operation,id responseObject) {

       NSString* str= [[NSStringalloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

       NSLog(@"%@", str);

    } failure:^(AFHTTPRequestOperation *operation,NSError *error) {

       NSLog(@"请求失败");

    }];

}


- (void)imageRequest{

    

   NSURLRequest*request = [NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://www.baidu.com/img/bdlogo.png"]];

    

   AFImageRequestOperation* operation = [[AFImageRequestOperationalloc] initWithRequest:request];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation*operation, id responseObject){

       self.view.backgroundColor = [UIColor colorWithPatternImage:responseObject];

    } failure:^(AFHTTPRequestOperation *operation,NSError *error) {

       NSLog(@"请求失败");

    }];

    [operation start];

}


- (void)jsonRequest{

   NSURLRequest*request = [NSURLRequestrequestWithURL:[NSURLURLWithString:@"https://api.douban.com/v2/book/search?q=harry&start=0&apikey=08e5e8516f3402a71ab1b8f44d9c07f5"]];

   AFJSONRequestOperation* operation = [[AFJSONRequestOperationalloc] initWithRequest:request];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation*operation, id responseObject){

       NSNumber*num = [responseObject objectForKey:@"count"];

       NSLog(@"%@", num);

    } failure:^(AFHTTPRequestOperation *operation,NSError *error) {

       NSLog(@"请求失败");

    }];

    [operation start];

}


- (void)httpRequest{

    

   NSURLRequest*request = [NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://10.0.8.8/sns/my/user_list.php"]];

    

   AFHTTPRequestOperation* operation = [[AFHTTPRequestOperationalloc] initWithRequest:request];

   //设置回调block

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation*operation, id responseObject){

       NSString* str= [[NSStringalloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

       NSLog(@"%@", str);

    } failure:^(AFHTTPRequestOperation *operation,NSError *error) {

       NSLog(@"请求失败");

    }];

    //开始

    [operation start];

}








@end


0 0
原创粉丝点击