+(void)taoqipost:(NSString *)url params:(NSMutableDictionary *)params controller:(UIViewController *)VC success:(void (^)(id _Nullable))success errcode:(void (^)(id _Nullable))errcode failure:(void (^)(NSError * _Nullable))failure
{
// 1.創建請求管理者
AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];//請求數據格式為json
manager.responseSerializer = [AFJSONResponseSerializer serializer];//響應數據格式
manager.requestSerializer.timeoutInterval = 10.0;//超時時間10秒
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:[NSSet setWithObjects:@"application/json", @"text/json" , @"text/html", @"text/plain", @"text/javascript", nil]];
NSDictionary *commonParam = [NSDictionary dictionaryWithObjectsAndKeys:
gDevId, @"devid",
gToken, @"token",
[NSNumber numberWithInt:DEV_TYPE], @"devtype",
[NSNumber numberWithInt:APPVER], @"appver",
[NSNumber numberWithInt:APP_ID], @"appid",
nil];
NSLog(@"token==%@",gToken);
NSMutableDictionary *bodyDic = [NSMutableDictionary dictionaryWithDictionary:commonParam];//字典合并
for (id key in params) {
id value=[params objectForKey:key];
[bodyDic setObject:value forKey:key];
}
NSLog(@"拼接的字典%@",bodyDic);
[manager POST:url parameters:bodyDic progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if ([responseObject[@"errcode"] integerValue]==0) {
if (success) {
success(responseObject);//code==0的時候傳出去直接解析
}
}else{
if (errcode) {
errcode(responseObject[@"errcode"]);//code!=0的時候只要傳出錯誤碼就行
}
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);//網絡錯誤等等
}
}];
網絡請求封裝錯誤碼
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
推薦閱讀更多精彩內容
- 代碼地址https://github.com/chenwei007/AFN-.git 中心化的設計思想,代碼簡潔統...
- React Native中雖然也內置了XMLHttpRequest 網絡請求API(也就是俗稱的ajax),但XM...
- 網絡請求AFNetworking使用量不用過多的說了,但是在開發過程中,需要用到自己去封裝一個網絡請求,此處主要是...