网址:https://github.com/AFNetworking/AFNetworking
Swift网络请求的调用:http://swiftcafe.io/2015/12/20/nsurlsession/
OC 网络请求的调用:http://stackoverflow.com/questions/19099448/send-post-request-using-nsurlsession
#define API_URL_BASE @"" //#define API_URL_BASE @"https://api.app.net/" //#define API_Test @"stream/0/posts/stream/global" #define API_UserInfor @"" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; /** * https://github.com/AFNetworking/AFNetworking */ [self createAF3RequestData]; } #pragma mark - AF3.0数据请求 - (void)createAF3RequestData{ AFHTTPSessionManager *manager =[[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:API_URL_BASE]]; manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone]; /*! * @brief GET */ //[ manager GET:@"stream/0/posts/stream/global" parameters:nil progress:nil success:^(NSURLSessionDataTask * __unused task, id JSON) { //NSLog(@"返回数据%@",JSON); //} failure:^(NSURLSessionDataTask *__unused task, NSError *error) { //}]; /*! * @brief Post */ NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setObject:@"1018" forKey:@"user_id"]; [dic setObject:@"180" forKey:@"user_depid"]; [manager POST:API_UserInfor parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"%@",responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }]; } @end
引用他人资源:http://my.oschina.net/u/2252300/blog/615951
最近更新了AFNetworking的版本,用了最新的3.0.4
发现较之前使用的2.5版本有较大的区别
在文件目录结构上3.0.4去除了NSURLConnection这个文件夹和里面的
AFHTTPRequestOperation.h
AFHTTPRequestOperation.m
AFHTTPRequestOperationManager.h
AFHTTPRequestOperationManager.m
AFURLConnectionOperation.h
AFURLConnectionOperation.m
这6个文件
在之前的2.5版本中我们常使用的方法是:
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; mgr.requestSerializer = [AFHTTPRequestSerializer serializer]; mgr.responseSerializer = [AFJSONResponseSerializer serializer]; NSDictionary *params = @{ @"PPID":[NSString stringWithFormat:@"%ld", (long)ppId], @"UserID":userId, }; [mgr POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSDictionary* jsonRsp = (NSDictionary *)responseObject; if (isSuccessRsp(jsonRsp)) { success(jsonRsp); } else { failure(errorMsgRsp(jsonRsp)); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { failure(error.description); }];
- (AFHTTPRequestOperation *)POST:(NSString *)URLString headParameters:(id)headParameters bodyParameters:(id)bodyParameters success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure { [self setHeadParameters:headParameters]; NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString] absoluteString] parameters:bodyParameters error:nil]; AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; [self.operationQueue addOperation:operation]; return operation;
同时也不需要手动写队列
现3.0.4使用方法更加简单
- (void)POST:(NSString *)URLString headParameters:(id)headParameters bodyParameters:(id)bodyParameters success:(void (^)( id responseObject))success failure:(void (^)( NSError *error))failure { NSURL *url= [NSURL URLWithString:URLString]; if (url == NULL) { return; } AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager]; if (headParameters) { [self setHeadParameters:headParameters sessionMgr:sessionManager]; } [sessionManager POST:[url absoluteString] parameters:bodyParameters progress:^(NSProgress * _Nonnull uploadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { success(responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { failure(error); }]; }