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)

时间: 2024-08-01 22:44:57

AFNetWorking的相关文章

ios AFNetworking 3.0 原码阅读分析 (一)(AFURLRequestSerialization)

本文主要内容是讲AFNetworking中的AFURLRequestSerialization.它主要的作用是在我们要发送一个网络请求的时候帮助我们创NSMutableURLRequest并封装好所需要的参数到NSMutableURLRequest中.那它内部做了些什么,提供了什么功能,使得我们进行网络请求时候变得如此方便.简单.好像我们什么都不用管就能建立一个正确的请求体NSURLRequest.接下来就会一步步揭开它神秘的面纱. 概览 首先看一下在AFURLRequestSerializat

AFNetworking 3.0x版本最新特性

AFNetworking是一款在OS X和iOS下都令人喜爱的网络库.为了迎合iOS新版本的升级, AFNetworking在3.0版本中删除了基于 NSURLConnection API的所有支持.如果你的项目以前使用过这些API,建议您立即升级到基于 NSURLSession 的API的AFNetworking的版本.本指南将引导您完成这个过程. 本指南是为了引导使用AFNetworking 2.x升级到最新的版本API,以达到过渡的目的,并且解释了新增和更改的设计结构. 新设备要求: iO

AFNetworking发送和接收字符串时报3840错误的解决办法

AFNetworking框架默认请求类型和响应类型都是JSON格式的,在特殊情况下,我们使用字符串形式的时候,就会报3840错误,如何解决呢? 设置请求管理者: // 因为传递过去和接收回来的数据都不是json类型的,所以在这里要设置为AFHTTPRequestSerializer和AFHTTPResponseSerializer mgr.requestSerializer = [AFHTTPRequestSerializer serializer];// 请求 mgr.responseSeri

iOS开发 之 AFNetworking的基本使用

首先下载AFNetworking 然后在需要使用的类中,导入如下2个头文件: #import "AFNetworking.h"//主要用于网络请求方法 #import "UIKit+AFNetworking.h"//里面有异步加载图片的方法 GET请求网络数据方式: -(void)obtainData {     // 启动系统风火轮     [UIApplication sharedApplication].networkActivityIndicatorVisi

AFNetworking imageView button设置图片缓存

如果对AFNetworking 下载的图片不进行缓存,可能会导致每次加载  image图片的时候都会重新 下载图片; 严重浪费资源: AFNetworking有自带的很方便的配置缓存图片的方法: 在 UIKit+AFNetworking中, imageView和Button的类目都可以设置缓存: 方法如下: button [UIButton setSharedImageCache:[UIButton sharedImageCache]]; imageView [UIImageView setSh

AFNetworking 2.5.0版本的使用

http://afnetworking.com/ http://cocoadocs.org/docsets/AFNetworking/2.5.0/ 1. 下载源码并进行编译 源码地址 http://pan.baidu.com/s/1jG24w3W 2. 判断当前是否有网络,基准网址为 http://baidu.com/ 源码: 可以用来监测WWAN或者WiFi或者断网状态;) // 基准baseURL NSURL *baseURL = [NSURL URLWithString:@"http://

AFNetworking的使用

AFN  1 AFN的框架结构 NSURLSession: NSURLSessionManager(对NSURLSession的封装); AFHttpSessionManager(会话管理者) 序列化处理serialization: AFURLRequestSerialization(请求); AFURLResponseSerialization(响应) 扩展功能: AFSecurityPolicy(安全); AFNetworkReachabilityManager(监听) UIKit+AFNe

iOS 网络编程:AFNetworking

1 简介 1.1 概念 AFNetworking网络框架并不是IOS自带的框架,而是第三方的开源框架.它是对NSURLConnection和NSURLSession API的封装,但是目前AFNetworking 3.0已经删除了基于 NSURLConnection API的所有支持,所以本文只记录基于NSURLSession API的相关接口.AFNetworking 框架是基于Object-C语言,若需要使用Swift语言版可以了解Alamofire框架. 个人感觉学习AFNetworkin

ios开发中-AFNetworking 的简单介绍

Blog: Draveness 关注仓库,及时获得更新: iOS-Source-Code-Analyze 在这一系列的文章中,我会对 AFNetworking 的源代码进行分析,深入了解一下它是如何构建的,如何在日常中完成发送 HTTP 请求.构建网络层这一任务. AFNetworking 是如今 iOS 开发中不可缺少的组件之一.它的 github 配置上是如下介绍的: Perhaps the most important feature of all, however, is the ama

AFNetworking 3.0迁移指南

AFNetworking是一款在OS X和iOS下都令人喜爱的网络库.为了迎合iOS新版本的升级, AFNetworking在3.0版本中删除了基于 NSURLConnection API的所有支持.如果你的项目以前使用过这些API,建议您立即升级到基于 NSURLSession 的API的AFNetworking的版本.本指南将引导您完成这个过程. 本指南是为了引导使用AFNetworking 2.x升级到最新的版本API,以达到过渡的目的,并且解释了新增和更改的设计结构. 新设备要求: iO