AFNetWorking 网络请求

一.文件的下载.

/**1.图片请求地址,请求地址可以是视频,应用,都可以*/
    NSURL *url = [NSURL URLWithString:@"https://ss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=34695d10ffedab6474271e80910b9bf1/9d82d158ccbf6c81106c0f33b83eb13532fa4095.jpg"];

/**2.开始请求*/
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

/**3.加载请求*/
    _operstion = [[AFHTTPRequestOperation alloc] initWithRequest:request];

/**4.文件地址,存储地址,text是文件名*/

    NSString *path = [NSString stringWithFormat:@"%@/Documents/text",NSHomeDirectory()];
 /**4.1打印沙盒路径*/
    NSLog(@"%@",NSHomeDirectory());

/**5.转换成url*/
    url = [NSURL fileURLWithPath:path];

/**6.输出流,加载url*/
    _operstion.outputStream = [NSOutputStream outputStreamWithURL:url append:NO];

/**7.下载进度,总进度totalBytesRead,当前进度totalBytesExpectedToRead*/
    [_operstion setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {

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

/**8.设置回调,请求的数据responseObject*/
    [_operstion setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"下载成功");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
    }];
    /**8.1开始下载*/
    [_operstion start];
}
/**下面是xib回调方法,只有加载xib后可以实现*/
- (IBAction)start:(id)sender {
    /**1.开始下载*/
    [_operstion start];
}

- (IBAction)pause:(id)sender {
    /**2.暂停下载*/
    [_operstion pause];
}

- (IBAction)continue:(id)sender {
    /**3.继续下载*/
    [_operstion resume];
}

二.请求数据(地址可以是不正规的json)

   /**1.请求地址*/
    NSURL *url = [NSURL URLWithString:@"http://1000phone.net:8088/qfapp/index.php/juba/news/get_news_list"];

    /**2.创建请求加载地址*/
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    /**3.加载请求*/
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    /**4.设置回调,请求的数据responseObject是NSData格式的*/
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"http->%@",responseObject);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求失败");
    }];

    /**5.开始请求*/
    [operation start];

三.请求正确Json格式的数据

    NSURL *url = [NSURL URLWithString:@"json格式文件"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

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

        NSLog(@"%@",responseObject);


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求失败");
    }];
    [operation start];

四.请求图片

/**1.请求地址*/
    NSURL *url = [NSURL URLWithString:@"https://ss1.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=01a88dd7229759ee4a05338bd4c67724/0eb30f2442a7d933c1c70da9a94bd11372f001f8.jpg"];

 /**2.加载请求地址*/
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
/**3.加载请求*/
    AFImageRequestOperation *operation = [[AFImageRequestOperation alloc] initWithRequest:request];
  /**4.请求回调的方法*/
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

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

        NSLog(@"%@",responseObject);

  /**4.1图片转换成颜色*/
        self.view.backgroundColor = [UIColor colorWithPatternImage:responseObject];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求失败");
    }];
 /**5.开始请求*/
    [operation start];`

五.get和post请求

 /**1.请求地址*/
    NSURL *url = [NSURL URLWithString:@"http://10.0.8.8/sns/"];
/**2.加载请求地址*/
    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:url];
 /**3.get请求*/
    /**get请求,拼接的参数my/user_list.php?count=28*/
    [client getPath:@"my/user_list.php?count=28" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"------->%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求失败");
    }];
 /**/**或者3.post请求*/
    /**请求参数@{@"count":@"20"}*/
    [client postPath:@"my/user_list.php" parameters:@{@"count":@"20"} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"d");
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求失败");
    }];*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值