分享一下我的网络封装工具类

首先呢,创建一个HttpTool

.h文件里

import

import “HttpTool.h”

import “AFNetworking.h”

static NSString * kBaseUrl = SERVER_HOST;

@interface AFHttpClient : AFHTTPSessionManager

  • (instancetype)sharedClient;

@end

@implementation AFHttpClient

  • (instancetype)sharedClient {

    static AFHttpClient * client = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

    NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    client = [[AFHttpClient alloc] initWithBaseURL:[NSURL URLWithString:kBaseUrl] sessionConfiguration:configuration];
    
    //接收参数类型
    client.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",@"text/json",@"text/javascript",@"text/plain",@"image/gif", nil];
    
    //设置超时时间
    client.requestSerializer.timeoutInterval = 15;
    //安全策略
    client.securityPolicy = [AFSecurityPolicy defaultPolicy];
    

    });

    return client;
    }

@end

@implementation HttpTool

  • (void)getWithPath:(NSString *)path
    params:(NSDictionary *)params
    success:(HttpSuccessBlock)success
    failure:(HttpFailureBlock)failure {
    //获取完整的url路径
    NSString * url = [kBaseUrl stringByAppendingPathComponent:path];

    [[AFHttpClient sharedClient] GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {

    success(responseObject);
    

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    failure(error);
    

    }];

}

  • (void)postWithPath:(NSString *)path
    params:(NSDictionary *)params
    success:(HttpSuccessBlock)success
    failure:(HttpFailureBlock)failure {
    //获取完整的url路径
    NSString *url = [[NSString alloc] init];
    if ([path containsString:@”https://”]) {
    url = path;
    } else {
    url = [kBaseUrl stringByAppendingPathComponent:path];
    }
    // NSString * url = [kBaseUrl stringByAppendingPathComponent:path];

    [[AFHttpClient sharedClient] POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {

    success(responseObject);
    

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    failure(error);
    

    }];

}

  • (void)downloadWithPath:(NSString *)path
    success:(HttpSuccessBlock)success
    failure:(HttpFailureBlock)failure
    progress:(HttpDownloadProgressBlock)progress {

    //获取完整的url路径
    NSString * urlString = [kBaseUrl stringByAppendingPathComponent:path];

    //下载
    NSURL *URL = [NSURL URLWithString:urlString];

    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURLSessionDownloadTask downloadTask = [[AFHttpClient sharedClient] downloadTaskWithRequest:request progress:^(NSProgress _Nonnull downloadProgress) {

    progress(downloadProgress.fractionCompleted);
    

    } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

    //获取沙盒cache路径
    NSURL * documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    
    return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
    

    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {

    if (error) {
        failure(error);
    } else {
        success(filePath.path);
    }
    

    }];

    [downloadTask resume];

}

  • (void)uploadImageWithPath:(NSString *)path
    params:(NSDictionary *)params
    thumbName:(NSString *)imagekey
    image:(UIImage *)image
    success:(HttpSuccessBlock)success
    failure:(HttpFailureBlock)failure
    progress:(HttpUploadProgressBlock)progress {

    //获取完整的url路径
    NSString * urlString = [kBaseUrl stringByAppendingPathComponent:path];

    NSData * data = UIImagePNGRepresentation(image);

    [[AFHttpClient sharedClient] POST:urlString parameters:params constructingBodyWithBlock:^(id _Nonnull formData) {

    [formData appendPartWithFileData:data name:imagekey fileName:@"01.png" mimeType:@"image/png"];
    

    } progress:^(NSProgress * _Nonnull uploadProgress) {

    progress(uploadProgress.fractionCompleted);
    

    } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {

    success(responseObject);
    

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    failure(error);
    

    }];
    }

@end

再创建一个APIConfigure

ifndef APIConfigure_h

define APIConfigure_h

//#define SERVER_HOST @””
//#define ICON_HOST @”“

POST请求就这么简单,OK完事

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值