首页 > 移动平台 > 详细

IOS-网络(AFNetworking)

时间:2016-02-11 19:14:25      阅读:346      评论:0      收藏:0      [点我收藏+]

一、AFNetWorking基本使用

  1 //
  2 //  ViewController.m
  3 //  IOS_0112_AFNetWorking
  4 //
  5 //  Created by ma c on 16/2/11.
  6 //  Copyright © 2016年 博文科技. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 #import "AFNetworking.h"
 11 
 12 @interface ViewController ()
 13 
 14 @end
 15 
 16 @implementation ViewController
 17 
 18 - (void)viewDidLoad {
 19     [super viewDidLoad];
 20     self.view.backgroundColor = [UIColor cyanColor];
 21     
 22 }
 23 
 24 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 25 {
 26     [self getData];
 27 }
 28 
 29 - (void)postJSON
 30 {
 31     // 1.创建一个请求操作管理者
 32     AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
 33     // 2.声明:等会服务器返回的是JSON数据,默认是JSON数据
 34     mgr.responseSerializer = [AFJSONResponseSerializer serializer];
 35     
 36     // 2.请求参数
 37     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
 38     dict[@"username"] = @"123";
 39     dict[@"pwd"] = @"123";
 40     // 3.发送一个GET请求
 41     NSString *url = @"http://localhost:8080/MJServer/login";
 42     [mgr POST:url parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {
 43         ;
 44     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
 45         //判断responseObject的类型
 46         //NSLog(@"请求成功-----%@",[responseObject class]);
 47         NSLog(@"请求成功-----%@", responseObject);
 48     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
 49         NSLog(@"请求失败");
 50     }];
 51 
 52     
 53 }
 54 
 55 - (void)getData
 56 {
 57     // 1.创建一个请求操作管理者
 58     AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
 59     // 2.声明:不要对服务器返回的数据进行解析,直接返回data即可
 60     mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
 61     
 62     // 2.请求参数
 63     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
 64     dict[@"username"] = @"123";
 65     dict[@"pwd"] = @"123";
 66     //dict[@"type"] = @"XML";
 67     // 3.发送一个GET请求
 68     NSString *url = @"http://localhost:8080/MJServer/login";
 69     [mgr GET:url parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {
 70         ;
 71     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
 72         
 73         NSLog(@"请求成功-----%@", [responseObject class]);
 74         
 75         NSDictionary *dcit = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
 76         NSLog(@"%@",dict);
 77         
 78     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
 79         NSLog(@"请求失败");
 80     }];
 81 
 82     
 83 }
 84 - (void)getXML
 85 {
 86     // 1.创建一个请求操作管理者
 87     AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
 88     // 2.声明:等会服务器返回的是XML数据
 89     mgr.responseSerializer = [AFXMLParserResponseSerializer serializer];
 90     // 2.请求参数
 91     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
 92     dict[@"username"] = @"123";
 93     dict[@"pwd"] = @"123";
 94     dict[@"type"] = @"XML";
 95     // 3.发送一个GET请求
 96     NSString *url = @"http://localhost:8080/MJServer/login";
 97     [mgr GET:url parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {
 98         ;
 99     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
100 
101         NSLog(@"请求成功-----%@", responseObject);
102         //解析XML数据
103         
104     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
105         NSLog(@"请求失败");
106     }];
107 
108 }
109 - (void)getJSON
110 {
111     // 1.创建一个请求操作管理者
112     AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
113     // 2.声明:等会服务器返回的是JSON数据,默认是JSON数据
114     mgr.responseSerializer = [AFJSONResponseSerializer serializer];
115 
116     // 2.请求参数
117     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
118     dict[@"username"] = @"123";
119     dict[@"pwd"] = @"123";
120     // 3.发送一个GET请求
121     NSString *url = @"http://localhost:8080/MJServer/login";
122     [mgr GET:url parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {
123         ;
124     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
125         //判断responseObject的类型
126         //NSLog(@"请求成功-----%@",[responseObject class]);
127         NSLog(@"请求成功-----%@", responseObject);
128     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
129         NSLog(@"请求失败");
130     }];
131 
132 }
133 @end

 

IOS-网络(AFNetworking)

原文:http://www.cnblogs.com/oc-bowen/p/5186588.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!