由于之前不太會用簡書寫文章,第一次寫出來的東西不規(guī)整.現(xiàn)在重新整理一下.
對于AFNetworking的網(wǎng)絡(luò)請求,大家應(yīng)該都不陌生.經(jīng)過實際的開發(fā)和自己的總結(jié),現(xiàn)分享一套比價方便實用的封裝請求.當(dāng)然,每個項目的網(wǎng)絡(luò)對接各異,這里也只是做出一個樣例的展示,我們還是要根據(jù)自己項目的實際情況來封裝AFN方法.
.h文件
#importtypedef NS_ENUM(NSUInteger,HttpRequestType) {
HttpRequestTypeGet = 0,//get
HttpRequestTypePost//post
};
@interface GlobelRequest : NSObject
/**
* 發(fā)送get請求
* @param URLString 請求的網(wǎng)址字符串
* @param parameters 請求的參數(shù)
* @param success 請求成功的回調(diào)
* @param failure 請求失敗的回調(diào)
*/
+ (void)getWithURLString:(NSString *)URLString parameters (id)parameters success:(void (^)(NSDictionary * responseObject))success failure:(void (^)(NSError *error))failure;
/**
* 發(fā)送post請求
* @param URLString 請求的網(wǎng)址字符串
* @param parameters 請求的參數(shù)
* @param success 請求成功的回調(diào)
* @param failure 請求失敗的回調(diào)
*/
+ (void)postWithURLString:(NSString *)URLString parameters (id)parameters success:(void (^)(NSDictionary * responseObject))success failure:(void (^)(NSError *error))failure;
/**
* 發(fā)送網(wǎng)絡(luò)請求---get/post 通用
* @param URLString 請求的網(wǎng)址字符串
* @param parameters 請求的參數(shù)
* @param type 請求的類型
* @param resultBlock 請求的結(jié)果
*/
+ (void)requestWithURLString:(NSString *)URLString parameters:(id)parameters type:(HttpRequestType)type success:(void (^)(NSDictionary * responseObject))success failure:(void (^)(NSError * error))failure;
@end
.m文件
#import "GlobelRequest.h"
#import "AFNetworking.h"
@implementation GlobelRequest
#pragma mark -- GET請求 --
+ (void)getWithURLString:(NSString *)URLString parameters:(id)parameters success:(void (^)(NSDictionary * responseObject))success failure:(void (^)(NSError *))failure {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 可以接受的類型
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// 請求隊列的最大并發(fā)數(shù)
// manager.operationQueue.maxConcurrentOperationCount = 5;
// 請求超時的時間
// manager.requestSerializer.timeoutInterval = 5;
[manager GET:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
NSDictionary * dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
success(dictionary);
}} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}}];
}
#pragma mark -- POST請求 --
+ (void)postWithURLString:(NSString *)URLString parameters:(id)parameters success:(void (^)(NSDictionary * responseObject))success failure:(void (^)(NSError *error))failure{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
NSDictionary * dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
success(dictionary);
//success(responseObject);
}} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}}];
}
#pragma mark -- POST/GET網(wǎng)絡(luò)請求 --
+ (void)requestWithURLString:(NSString *)URLString
parameters:(id)parameters
type:(HttpRequestType)type
success:(void (^)(NSDictionary * responseObject))success
failure:(void (^)(NSError * error))failure {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//manager.requestSerializer = [AFJSONRequestSerializer serializer];
// [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];//此句代碼酌情增加.
switch (type) {
case HttpRequestTypeGet:
{ [manager GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
NSDictionary * dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
success(dictionary);
}} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}}];
}
break;
case HttpRequestTypePost:
{
[manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
NSDictionary * dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
success(dictionary);
//success(responseObject);
}} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}}];
}
break;
}
}
@end
這里是AFNetworking的常用封裝.關(guān)于圖片上傳的方法暫未涉及.往后會繼續(xù)總結(jié).篇幅不想寫太長,第二個問題請見下一篇簡書.