码迷,mamicode.com
首页 > Web开发 > 详细

AFNetWroking 3.0 GET&POST基本使用

时间:2016-04-29 14:02:11      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

```
- (void)requestWithUrl:(NSString *)url params:(NSDictionary *)params methodType:(NSString *)methodType{
    // 1. 创建URLString
    if (url == nil) {
      url = [[IMConfigManager defaultManager] getNewsAppUrl];
    }

    // 2. 创建SessionConfiguration
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    // 3. 创建SessionManager
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
    // 4. 创建Request
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:methodType URLString:url parameters:params error:nil];

    // 5. 设置Response
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    
    if ([methodType isEqualToString:@"POST"]) {
        // 设置application的HTTPHeaderField
        [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    }

    // 6. 创建SessionDataTask
    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            [self requestFailure:error];
        } else {

            [self requestSuccess:response responseObject:responseObject];
        }
    }];
    
    // 7. 执行SessionDataTask
    [dataTask resume];
}

```

 

AFNetWroking 3.0 GET&POST基本使用

标签:

原文地址:http://www.cnblogs.com/bridge-wuxl/p/5445552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!