美文网首页
AFNetWorking GET 和 POST请求 封装

AFNetWorking GET 和 POST请求 封装

作者: 然然啊 | 来源:发表于2015-11-21 16:07 被阅读3488次

    GET请求

    WangLuoQingQiu.h

    #import <Foundation/Foundation.h>
    #import "BaseHeader.h"
    typedef void(^AFNData)(id block);
    
    typedef void(^AFNDataa)(id BLOCK);
    
    @interface WangLuoQingQiu : NSObject
    
    + (void)getdataWithString:(NSString *)string block:(AFNData)block BLOCK:(AFNDataa)BLOCK;
    
    @end
    

    WangLuoQingQiu.m

    #import "WangLuoQingQiu.h"
    @implementation WangLuoQingQiu
    
    + (void)getdataWithString:(NSString *)string block:(AFNData)block BLOCK:(AFNDataa)BLOCK
    {
        AFNetworkReachabilityManager *netWorkingManager = [AFNetworkReachabilityManager sharedManager];
        NSString *url_string = [NSString stringWithFormat:@"%@", string];
        NSURL *URL = [NSURL URLWithString:url_string];
        url_string = [url_string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"text/json", @"application/json", @"text/javascript", @"text/html",  nil];
        [manager GET:URL.absoluteString parameters:nil success:^(NSURLSessionTask *task, id responseObject) {
            [netWorkingManager stopMonitoring];
            block(responseObject);
    //        NSLog(@"JSON: %@", responseObject);
        } failure:^(NSURLSessionTask *operation, NSError *error) {
            NSLog(@"Error: %@", error);
            BLOCK (operation);
        }];
    
    }
    
    @end
    

    调用方法

    - (void)getData:(NSString *)url
    {
        [WangLuoQingQiu getdataWithString:url block:^(id block) {
            //block是请求回来的数据,是个字典或者数组
        } BLOCK:^(id BLOCK) {
            //网络请求失败执行的方法
        }];
    }
    

    POST请求

    AFNPostBlock.h

    #import <Foundation/Foundation.h>
    #import <AFNetworking.h>
    typedef void(^AFNData)(id block);
    
    @interface AFNPostBlock : NSObject
    +(void)getDataWithString:(NSString *)string body:(NSDictionary *)parameters block:(AFNData)block;
    
    @end
    

    AFNPostBlock.m

    + (void)getDataWithString:(NSString *)string body:(NSDictionary *)parameters block:(AFNData)block
    {
        AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
        
        NSString *url_string = [NSString stringWithFormat:@"%@",string];
        url_string = [url_string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
        
        [manager POST:url_string parameters:parameters success:^(NSURLSessionTask *task, id responseObject)  {
            
            [netWorkManager stopMonitoring];
    
            block(responseObject);
            
        } failure:^(NSURLSessionTask *operation, NSError *error) {
    
            NSLog(@"失败 === %@",error);
               dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    
               NSLog(@"Error: %@", error);
            });
        }];
    }
    

    调用方法

    - (void)postDateUrl:(NSString *)url body:(NSDictionary *)body
    {
        [AFNPostBlock getDataWithString:url body:body block:^(id block) {
    //block 是请求回得数据
        }];
    }
    

    相关文章

      网友评论

          本文标题:AFNetWorking GET 和 POST请求 封装

          本文链接:https://www.haomeiwen.com/subject/ydalhttx.html