AFNetworking2.0简易GET,POST请求封装以及使用
封装 以及 请求 get 简易 post 使用
2023-09-14 08:57:16 时间
AFNetworking2.0简易GET,POST请求封装以及使用
AFNetworking不用我赘述其强大性,本人仅仅做了非常简易的封装,解决了有时候请求出错的问题,提供源码给大家。
封装源码库下载地址:
源码:
Networking.h 与 Networking.m
// // Networking.h // Weather // Created by YouXianMing on 15/01/01. // Copyright (c) 2014年 YouXianMing. All rights reserved. #import Foundation/Foundation.h #import "AFNetworking.h" @interface Networking : NSObject #pragma mark - GET请求 + (void)GET:(NSString *)urlString parameters:(id)parameters success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; #pragma mark - POST请求 + (void)POST:(NSString *)urlString parameters:(id)parameters success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; @end
// // Networking.m // Weather // Created by YouXianMing on 15/01/01. // Copyright (c) 2014年 YouXianMing. All rights reserved. #import "Networking.h" @implementation Networking + (void)GET:(NSString *)urlString parameters:(id)parameters success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // 防止解析不出来 manager.responseSerializer.acceptableContentTypes = \ [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; [manager GET:urlString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { if (success) { success(operation, responseObject); failure:^(AFHTTPRequestOperation *operation, NSError *error) { if (failure) { failure(operation, error); + (void)POST:(NSString *)urlString parameters:(id)parameters success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // 防止解析不出来 manager.responseSerializer.acceptableContentTypes = \ [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; [manager POST:urlString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { if (success) { success(operation, responseObject); failure:^(AFHTTPRequestOperation *operation, NSError *error) { if (failure) { failure(operation, error); @end
GET请求带参数与不带参数之间的转换:
POST请求:
源码修改的地方:
搭建一个包含多种Get请求和Post请求的工具类 在工作的过程中经常会遇到需要调用接口的场景,用得多了就写了一个请求的工具类,以后再遇到需要Get请求或者Post请求的情况直接调用就行。
相关文章
- Moleft专用函数库(一) - Curl最完整封装
- vue封装jquery修改自身以及兄弟元素的方法「建议收藏」
- vue自定义组件封装_vue组件的双向绑定实现
- 软件测试|selenium+python基础方法封装(一)
- 【Kotlin】Kotlin 面向对象 ( 类 | 成员变量 | 成员方法 | 封装 | 继承 | 多态 )
- Java学习笔记 基础知识(封装)
- 在Oracle中使用C程序员封装存储对象(c oracle存储对象)
- 分布式Redis封装最佳实践(分布式项目redis封装)
- 封装redis集群,实现构建高可用环境(redis集群的封装)
- ClassOfMarqueeScroll通用不间断滚动JS封装类
- 动态样式类封装JS代码