zoukankan      html  css  js  c++  java
  • AFNetWorking网络请求

    NetWorkAPIClient.h
    #import <Foundation/Foundation.h>
    #import "AFHTTPRequestOperationManager.h"
    
    #define POST_PATH @"/campus/dispatch.rpc"
    #define BASE_URL @"http://192.168.0.102:8080/idc/mobile/"//测试环境
    
    
    @interface NetWorkAPIClient : AFHTTPRequestOperationManager
    
    + (NetWorkAPIClient *)sharedClient;
    
    @end
    
    NetWorkAPIClient.m
    #import "NetWorkAPIClient.h"
    
    @implementation NetWorkAPIClient
    
    + (NetWorkAPIClient *)sharedClient {
        static NetWorkAPIClient *_sharedClient = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _sharedClient = [[NetWorkAPIClient alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL]];
        });
        
        return _sharedClient;
    }
    
    @end
    
    HttpRequestService.h
    typedef void (^SuccessBlock)(id result);
    typedef void (^FailedBlock)(NSError *error);
    
    + (void)postTest:(SuccessBlock)success failed:(FailedBlock)failed;
    
    HttpRequestService.m
    #import "NetWorkAPIClient.h"
    + (void)postTest:(SuccessBlock)success failed:(FailedBlock)failed
    {
        NSDictionary *parameters=[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%@",@"xxx"],@"userId",
                                 [NSString stringWithFormat:@"%@",nil],@"userName",
                                 [NSString stringWithFormat:@"%d",xxx],@"type",nil];
        [[NetWorkAPIClient sharedClient] POST:GET_USER_INFO_PATH parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            success(responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
        }];
    }
    
    // 使用
    -(void)requestUserInfoTest
    {
        
        [HttpRequestService postTest:^(id result)
         {
             NSDictionary *dataDic = result;
             NSLog(@"dataDic %@",dataDic);
             
         }failed:^(NSError *error)
         {
             
             
         }];
    }
  • 相关阅读:
    使用没有初始化变量很危险
    openssl动态库编译
    数据库水平切分及问题
    用dpkg命令制作deb包方法总结
    centOS安装mysql---glibc方式
    RabbitMQ常用命令
    shell脚本报错:-bash: xxx: /bin/bash^M: bad interpreter: No such file or directory
    mysql主从复制实现数据库同步
    CentOS 7.0下使用yum安装MySQL
    RabbitMQ的远程Web管理与监控工具
  • 原文地址:https://www.cnblogs.com/joesen/p/3564499.html
Copyright © 2011-2022 走看看