AFNetWorking

#import "ViewController.h"

#import "AFNetworking.h"

@interface ViewController (){

    AFHTTPRequestOperation *_operation;

}

- (IBAction)startRequest:(id)sender;

- (IBAction)start:(id)sender;

- (IBAction)pause:(id)sender;

- (IBAction)resume:(id)sender;

@end

 @implementation ViewController

 - (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    NSURL *url = [NSURL URLWithString:@"http://api.sina.cn/sinago/list.json?channel=news_toutiao"];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    //AFHTTPRequestOperation是基于NSURLRequest的

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

     //断点续传,下载存到本地

    //文件地址

    NSString *path = [NSString stringWithFormat:@"%@/Documents/text",NSHomeDirectory()];

    NSLog(@"%@",path);

    url = [NSURL fileURLWithPath:path];

    //输出流

    _operation.outputStream = [NSOutputStream outputStreamWithURL:url append:NO];

    //进度

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

        NSLog(@"%.2f",(float)totalBytesRead / totalBytesExpectedToRead);

    }];

        //设置回调 responseObject是请求成功的数据

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

        NSLog(@"下载成功");

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

        NSLog(@"下载失败");

    }];

 

}

- (void)startRequest:(id)sender{

    //[self httpRequest];

    //[self jsonRequest];

    //[self imageRequest];

    [self clientRequest];

}

- (void)clientRequest{

    NSURL *url = [NSURL URLWithString:@"http://10.0.8.8/sns/"];

    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:url];

    //get请求

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

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

        NSLog(@"%@",str);

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

        NSLog(@"请求失败");

    }];

    //post请求

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

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

        NSLog(@"%@",str);

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

        NSLog(@"请求失败");

    }];

}

- (void)imageRequest{

    NSURL *url = [NSURL URLWithString:@"http://static.mozilla.com.cn/static/thumbnail/_static_attachment_forum_201502_12_104146n5cyxnlkkyi4lukm.thumb_660_320.jpg"];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

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

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

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

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

        NSLog(@"请求失败");

    }];

    //开始请求

    [operation start];

//    加载图片

//    UIImageView *imageView; 

//    [imageView setImageWithURL:]

}

- (void)jsonRequest{

    NSURL *url = [NSURL URLWithString:@"http://api.sina.cn/sinago/list.json?channel=news_toutiao"];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    //要求必须是标准的json数据

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

    //设置回调

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

       //responseObject是json解析后的数组或字典

        NSLog(@"%@",responseObject);

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

        NSLog(@"请求失败");

    }];

    //开始请求

    [operation start];

}

 

- (void)httpRequest{

 

    NSURL *url = [NSURL URLWithString:@"http://api.sina.cn/sinago/list.json?channel=news_toutiao"];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    //AFHTTPRequestOperation是基于NSURLRequest的

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

    

    //设置回调 responseObject是请求成功的数据

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

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

        NSLog(@"%@",str);

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

        NSLog(@"请求失败");

    }];

    //开始请求

    [operation start];

}

 

- (void)start:(id)sender{

    [_operation start];

}

 

- (void)pause:(id)sender{

    [_operation pause];

}

 

- (void)resume:(id)sender{

    [_operation resume];

}

 

 

//MKNetWork(AFNetWorking,ASIHttpRequest)

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。