• AFN的初步封装(post、GET、有无参数)


    #import <Foundation/Foundation.h>
    
    #import <UIKit/UIKit.h>
    
    @interface MyURLPost : NSObject
    
    //POST 带参数
    +(void)post:(NSString *)url paramters:(NSDictionary *)param success:(void (^)(id responseObjt))success failure:(void (^)(NSError *error))failure;
    
    //发送GET请求
    + (void)get:(NSString *)url params: (NSDictionary *)params success:(void(^)(id responseObjt))success failure:(void(^)(NSError *error))failure;
    
    //发送POST请求
    + (void)post:(NSString *)url params:(NSDictionary *)params success:(void(^)(id responseObjt)) success failure:(void(^)(NSError *error))failure;
    
    //添加进度条
    +(void)post:(NSString *)url params:(NSDictionary *)params success:(void(^)(id responseObjt)) success failure:(void(^)(NSError *error))failure withView:(UIView *)view;
    
    @end
    #import "MyURLPost.h"
    #import "AFNetworking.h"
    #import "MYAlert.h"
    #import "NSObject+GetIP.h"
    #import "NSString+Hashing.h"
    @implementation MyURLPost
    
    //POST 带参数
    + (void)post:(NSString *)url paramters:(NSDictionary *)param success:(void (^)(id))success failure:(void (^)(NSError *errror))failure{
        //3.判断网络状况
        AFNetworkReachabilityManager *netManager = [AFNetworkReachabilityManager sharedManager];
        //开始监听
        [netManager startMonitoring];
        [netManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status){
            if (status == AFNetworkReachabilityStatusNotReachable)
            {
                //showAlert
           [MYAlert alertWithTitle:@"提示" messages:@"网络链接错误,请检查网络链接" cancelTitle:@"我知道了"  otherTitle:nil];
                DLog(@"没有网络");
                return;
            }else if (status == AFNetworkReachabilityStatusUnknown){
                DLog(@"未知网络");
            }else if (status == AFNetworkReachabilityStatusReachableViaWWAN){
                DLog(@"WiFi");
            }else if (status == AFNetworkReachabilityStatusReachableViaWiFi){
                DLog(@"手机网络");
            }
        }];
        
            NSTimeInterval time = [[NSDate  date]timeIntervalSince1970];
            long i = time;
            NSString *paramTime = [NSString stringWithFormat:@"%ld",i];
            NSString *ipstr = [NSObject  deviceIPAdress];
            NSString *paramIp = ipstr;
            NSString *signstr = [NSString  stringWithFormat:@"%@%@%@%@",paramTime,paramIp,@"phone_ios",@"d556bd3337cd909b49eb5e33f46ad65c"];
            NSString *md5str =  [signstr MD5Hash];
            NSString *mdstr = [md5str  lowercaseString];
            NSString *sign = mdstr;
            NSString *key = @"phone_ios";
            NSDictionary *dict = @{
                                   @"paramTime" : paramTime,
                                   @"paramIp" : paramIp,
                                   @"sign":sign,
                                   @"key" : key,
                                   @"param":param
                                   };
        
            AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            manager.responseSerializer =  [AFJSONResponseSerializer  serializer];
            manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
            [manager POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
                if (success) {
                    success(responseObject);
                }
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                if (failure) {
                    failure(error);
                }
            }];
    }
    
    //发送GET请求
    +(void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure{
        
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
        
        mgr.responseSerializer = [AFJSONResponseSerializer serializer];
        
        mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
        
        [mgr GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            if (success) {
                success(responseObject);
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            failure(error);
        }];
    }
    
    //发送POST请求
    + (void)post:(NSString *)url params:(NSDictionary *)params success:(void(^)(id responseObjt)) success failure:(void(^)(NSError *error))failure{
        
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
        
        mgr.responseSerializer = [AFJSONResponseSerializer serializer];
        
        mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
        
        //发送POST请求
        [mgr POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            if (success) {
                success(responseObject);
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            if (failure) {
                failure(error);
            }
        }];
    }
    
    
    +(void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure withView:(UIView *)view{
        
    }
    
    @end
    //使用实例
    //NSDictionary *params = @{
    //                         @"userId": userId,
    //                         
    //                         @"id":_numId
    //                         };
    //
    //DLog(@"%@",params);
    //
    //[MyURLPost post:confidefaultBankUrl paramters:params success:^(id responseObjt) {
    //    
    //    DLog(@"%@",responseObjt);
    //    
    //    if ([responseObjt[@"resultCode"] isEqualToString:@"100"]) {
    
    //        [self  findCard];
    //        
    //    }else{
    //        
    //    }
    //} failure:^(NSError *error) {
    //    
    //    DLog(@"%@",error);
    //}];
  • 相关阅读:
    深入浅出Win32多线程程序设计【2】线程控制
    深入浅出Win32多线程程序设计【1】基本概念
    在两个ASP.NET页面之间传递值
    Javascript基础
    DataGrid的几个小技巧
    推荐取代Visio的中国人的软件——Edraw
    ASP.NET如何防范SQL注入攻击
    软件版本号规定原则
    三层体系结构总结(三)
    .Net工具 .NET文档生成工具2.2
  • 原文地址:https://www.cnblogs.com/er-dai-ma-nong/p/5045732.html
Copyright © 2020-2023  润新知