标签:ict with har ace nsthread get init cal dict
众所周知。苹果搞的一套框架NSContention发送请求与接收请求的方式十分繁琐。操作起来非常不方便。
不仅要做区分各种请求设置各种不同的參数,并且还要常常在多线程里操作,同一时候还要对请求与返回的数据做各种序列化的操作。同一时候还要考虑请求数据的安全等一堆问题。
AFNetworking的出现:MAC/iOS设计的一套网络框架.(为了简化网络操作)
二、AFNetworking的使用
关于改动AFN源代码:通常序列化时做对text/plan等的支持时。能够一劳永逸的改动源代码,在acceptableContentTypes中改动就可以。
AFN进行GET、POST登录:
#pragma mark - get/post登录 - (void)getLogin { //1.管理器 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; //2.设置登录參数 NSDictionary *dict = @{ @"username":@"xn", @"password":@"123" }; //3.请求 [manager GET:@"http://localhost/login.php" parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"GET --> %@, %@", responseObject, [NSThread currentThread]); //自己主动返回主线程 } failure: ^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"%@", error); }]; } /** * 和上面的GET使用方法全然一样, 仅仅有一个POST參数不一样 */ - (void)postLogin { //1.管理器 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; //2.设置登录參数 NSDictionary *dict = @{ @"username":@"xn", @"password":@"123" }; //3.请求 [manager POST:@"http://localhost/login.php" parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"POST --> %@, %@", responseObject, [NSThread currentThread]); //自己主动返回主线程 } failure: ^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"%@", error); }]; }
#pragma mark - get 数据解析 - (void)getJSON { //1.请求管理器 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; //2.发起请求 [manager GET:@"http://localhost/videos.json" parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"%@", responseObject); } failure: ^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"%@", error); }]; } /** * 不支持XML数据解析 */ - (void)getXML { //1.管理器 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; //2.设置返回数据类型 manager.responseSerializer = [AFXMLParserResponseSerializer serializer]; //先实例化一下 //3.发起请求 [manager GET:@"http://localhost/videos.xml" parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"%@", responseObject); } failure: ^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"%@", error); }]; } - (void)getPlist { //1.管理器 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; //2.设置response类型 manager.responseSerializer = [AFPropertyListResponseSerializer serializer]; //是Response, 别写成request了. 改动为plist类型. manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"]; //这个能够直接往框架里面改动. //3.请求 [manager GET:@"http://localhost/videos.plist" parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"%@", responseObject); } failure: ^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"%@", error); }]; }
#pragma mark - post json数据与上传文件等 - (void)postJSON { //1.管理器 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; //2.设定类型. (这里要设置request-response的类型) manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; //这个决定了以下responseObject返回的类型 // manager.responseSerializer = [AFJSONResponseSerializer serializer]; // manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"]; //2.设置登录參数 NSDictionary *dict = @{ @"username":@"xn", @"password":@"123" }; //3.发送请求 [manager POST:@"http://localhost/postjson.php" parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) { // NSLog(@"postjson--> %@", responseObject); //这样显示JSON的话须要设置text/plain NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"%@",result); } failure: ^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"%@", error); }]; }
转载请注明出处:http://blog.csdn.net/xn4545945
标签:ict with har ace nsthread get init cal dict
原文地址:http://www.cnblogs.com/cxchanpin/p/6748814.html