标签:
1. AFN一套网络操作的第三方框架
a. NSURLConnection iOS2.0之后就有
b. NSURLSession iOS7.0之后才有
c. 默认支持序列化和反序列化json,xml需要自己解析 d. 优秀的错误处理机制
e. 封装了Reachability
f. 支持https
AFHTTPRequestOperationManager初始化的方法
○ self.requestSerializer = [AFHTTPRequestSerializer serializer];请求数据的序列化
○ self.responseSerializer = [AFJSONResponseSerializer serializer]; 返回数据的序列化
• <AFURLRequestSerialization>
发送给服务器的数据
二进制(默认)
json
plist 从服务器接收的数据
○ AFHTTPRequestSerializer
○ AFJSONRequestSerializer
○ AFPropertyListRequestSerializer
<AFURLResponseSerialization>
○ AFHTTPResponseSerializer
○ AFJSONResponseSerializer
○ AFXMLParserResponseSerializer
○ AFXMLDocumentResponseSerializer ○ AFPropertyListResponseSerializer ○ AFImageResponseSerializer
○ AFCompoundResponseSerializer
// get请求 不带参数
- (void)demo1{ NSString *str = @"http://127.0.0.1/demo11.json"; //创建一个管理者 AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager]; //管理者发起请求 [manager GET:str parameters:nil success:^void(AFHTTPRequestOperation *ope , id json) { NSLog(@"%@",json); } failure:^void(AFHTTPRequestOperation *op, NSError *error) { NSLog(@"%@",error); }]; }
//带参数的get
- (void)demo2{ NSString *str = @"http://127.0.0.1/php/login.php"; AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager]; //发送的参数 NSDictionary *dic = @{@"username":@"123&abc",@"password":@"abc"}; [manager GET:str parameters:dic success:^void(AFHTTPRequestOperation *ope , id json) { NSLog(@"%@",json); } failure:^void(AFHTTPRequestOperation *op, NSError *error) { NSLog(@"%@",error); }]; }
//带参数的post
- (void)demo3{ NSString *str = @"http://127.0.0.1/php/login.php"; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; //发送的参数 NSDictionary *dic = @{@"username":@"123&abc",@"password":@"abc"}; [manager POST:str parameters:dic success:^ void(AFHTTPRequestOperation *op , id json) { NSLog(@"%@",json); } failure:^ void(AFHTTPRequestOperation *op, NSError *error) { NSLog(@"%@",error); }]; }
//post上传文件
- (void)demo4{ NSString *str = @"http://127.0.0.1/upload/upload.php"; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:str parameters:nil constructingBodyWithBlock:^ void(id<AFMultipartFormData> formData) { // userfile //上传文件 NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"swifter.pdf" withExtension:nil]; [formData appendPartWithFileURL:fileUrl name:@"userfile" fileName:@"aaa.pdf" mimeType:@"application/pdf" error:nil]; } success:^ void(AFHTTPRequestOperation *op, id json) { NSLog(@"%@",json); } failure:^ void(AFHTTPRequestOperation *op, NSError *error) { NSLog(@"%@",error); }]; }
//设置解析响应的serializer 默认是JOSN 如果要返回html 或其它格式。必须要手动设置
- (void)demo5{ NSString *str = @"http://www.baidu.com"; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; //设置解析响应的serializer manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager GET:str parameters:nil success:^ void(AFHTTPRequestOperation *op, id data) { NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@",html); } failure:^ void(AFHTTPRequestOperation *op, NSError *error) { NSLog(@"%@",error); }]; }
//josn 以html 格式返回的代码
- (void)demo7{ NSString *str = @"http://c.m.163.com/nc/article/headline/T1348647853363/0-140.html"; AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil]; [manager GET:str parameters:nil success:^void(NSURLSessionDataTask *op, id data) { NSLog(@"%@",data); } failure:^ void(NSURLSessionDataTask *op, NSError *error) { NSLog(@"%@",error); }]; }
标签:
原文地址:http://www.cnblogs.com/deng1989/p/4621357.html