AFNetworking GET 请求 方法封装及调用

<pre name="code" class="objc"><span style="color:#ff0000;">在.h文件中</span>
#import <Foundation/Foundation.h>
#import <AFNetworking.h>
@interface MSHttpRequest : NSObject

+ (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure;
@end

<span style="color:#cc0000;">在.m文件中</span>
@implementation MSHttpRequest

+ (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
    
    static NSString *http = @"http://192.168.0.216:8080/app";
    NSString *str = [http stringByAppendingString:url];
   
    // 1.创建请求管理者
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];

    // 2.添加固定的请求头
    manger.requestSerializer = [AFJSONRequestSerializer serializer];
    manger.responseSerializer = [AFJSONResponseSerializer serializer];
    [manger.requestSerializer setValue:@"1.0" forHTTPHeaderField:@"app.version"];
    [manger.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"os"];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *loginUserId =  [userDefaults objectForKey:<pre name="code" class="objc" style="font-size: 18px;">user.login.id
]; NSLog(@"*************************%@",loginUserId); [manger.requestSerializer setValue:loginUserId forHTTPHeaderField:@"user.login.id"]; // 3.发送请求 [manger GET:str parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { if (success) { success(responseObject); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if (failure) { failure(error); } }];}

 
 

@end



   
 

在controller里面写请求方法即可  对应的参数给它

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo;">-(void)request{</p>NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
            params[@"type"] = @"1";
            params[@"sysState"] = @"0";
            params[@"pageSize"] = [NSString stringWithFormat:@"%ld",self.limite];
            [MSHttpRequest get:@"/api/freeOrder/getfreeList.json" params:params success:^(id json) {
                DLog(@"成功数据%@",json);
                _ListArr = [NSMutableArray array];
                _ListArr = json[@"data"][@"list"];
                _arrayFree = [[NSMutableArray alloc]init];
                for (NSDictionary *DTOdic in _ListArr) {
                    MSorderModel *model = [[MSorderModel alloc]init];
                    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
                  [_arrayFree addObject:dic];
                }
                [self.freeALLTableView reloadData];
            } failure:^(NSError *error) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"网络异常" message:@"确定" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
                [alertView show];
                DLog(@"失败原因%@",error);
            }];  
        }


你可能感兴趣的:(get,AFNetworking,请求)