AFNetworking 3.1

听说之后AFHttpWorking版本可能会影响到苹果的审核,今天下了最新版本的AFHttpWorking,并且做了简单的封装,我这里是通过cocoapods下载了两个工具

1=AFHttpWorking  2=JSONKit  为什么要下jsonkit勒,以前json解析都使用touchjson,偶然发现资料说jsonkit效率是第三方json解析中最高的,所以今天把它也下了来下,我这里只做了AFHttpWorking 的get/post/网络判断三个方法。下面我贴代码

.h文件

#import <Foundation/Foundation.h>
#import "AFDataDefine.h"
#import <AFNetworking/AFNetworking.h>
static NSString* const kAFAppDotNetAPIBaseURLString=@"http://www.xx.com";
@interface APIClient : AFHTTPSessionManager
+ (APIClient *)sharedClient;
-(void)getUrl:(NSString *)URLString parameters:(id)parameters call:(void (^)( RespInfo* info))callback;
-(void)postUrl:(NSString *)URLString parameters:(id)parameters call:(void (^)( RespInfo* info))callback;
-(void)checkNetWorkingIsOrNoUse: (void (^)( int StateOrType))callback;
@end

.m文件

#import "APIClient.h"
#import <JSONKit/JSONKit.h> #pragma mark - @implementation APIClient
+ (instancetype)sharedClient {
static APIClient *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[APIClient alloc] initWithBaseURL:[NSURL URLWithString:kAFAppDotNetAPIBaseURLString]];
_sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
_sharedClient.requestSerializer.timeoutInterval =20; }); _sharedClient.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain",@"charset=UTF-8",@"Content-Type",@"application/json",nil];; return _sharedClient;
} #pragma mark - /**
* 网络监测(在什么网络状态)
*
* @callback 1 未知网络
* @callback 2 无网络
* @callback 3 蜂窝数据网
* @callback 4 WiFi网络
*/ -(void)checkNetWorkingIsOrNoUse: (void (^)( int StateOrType))callback
{
// 创建网络监测者 [ [AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status)
{
case AFNetworkReachabilityStatusUnknown:
callback(1);
break; case AFNetworkReachabilityStatusNotReachable:
callback(2);
break; case AFNetworkReachabilityStatusReachableViaWWAN:
callback(3); break;
case AFNetworkReachabilityStatusReachableViaWiFi:
callback(4);
break; default: break;
}
}] ; [[AFNetworkReachabilityManager sharedManager] startMonitoring]; } -(void)postUrl:(NSString *)URLString parameters:(id)parameters call:(void (^)( RespInfo* info))callback
{ //首先判断是否有网络
[self checkNetWorkingIsOrNoUse:^(int StateOrType) {
if (StateOrType==2) { RespInfo* retobj = [[RespInfo alloc]init];
retobj.mBSuccess=NO;
NSString *AlertInfo=@"请检查网络是否畅通。";
retobj.mMsg=AlertInfo;
callback(retobj); } else
{ [self POST:URLString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; // 开启状态栏动画 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSString *JsonStr=[responseObject JSONString];
NSDictionary *MyDic=[JsonStr objectFromJSONString];
//申明block返回的累
RespInfo* retobj = [[RespInfo alloc]init];
//判断返回是否成功
NSString *IsOrNoSuccess=[MyDic objectForKey:@"Success"];
if ([IsOrNoSuccess intValue]==1) {
retobj.mBSuccess=YES;
//这里判断返回类型,如果为0不去取mObject的值,否则要取
int JudgeInt=[[MyDic objectForKey:@"ControlType"]intValue];
if (JudgeInt==0) {//只取列表值 NSString *DataListArrayStr=[MyDic objectForKey:@"Rows"];
retobj.listData=[DataListArrayStr objectFromJSONString]; }
else if(JudgeInt==1)//字取附加字典值
{
NSString *addicationDic=[MyDic objectForKey:@"AddicationDictionary"];
retobj.mObjectDictionary=[addicationDic objectFromJSONString];
}
else if(JudgeInt==2)//两个都取
{ NSString *DataListArrayStr=[MyDic objectForKey:@"Rows"];
retobj.listData=[DataListArrayStr objectFromJSONString]; NSString *addicationDic=[MyDic objectForKey:@"AddicationDictionary"];
retobj.mObjectDictionary=[addicationDic objectFromJSONString];
}
//返回的公用提示消息
NSString *AlertInfo=[MyDic objectForKey:@"Msg"];
retobj.mMsg=AlertInfo; }
else
{ retobj.mBSuccess=NO; NSString *AlertInfo=[MyDic objectForKey:@"Msg"];
retobj.mMsg=AlertInfo; } callback(retobj); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { RespInfo* retobj = [[RespInfo alloc]init];
retobj.mBSuccess=NO; retobj.mMsg=error.domain; }]; }
}]; } -(void)getUrl:(NSString *)URLString parameters:(id)parameters call:(void (^)( RespInfo* info))callback
{
//首先判断是否有网络
[self checkNetWorkingIsOrNoUse:^(int StateOrType) {
if (StateOrType==2) { RespInfo* retobj = [[RespInfo alloc]init];
retobj.mBSuccess=NO;
NSString *AlertInfo=@"请检查网络是否畅通。";
retobj.mMsg=AlertInfo;
callback(retobj); } else
{ [self GET:URLString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; // 开启状态栏动画 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; NSString *JsonStr=[responseObject JSONString];
NSDictionary *MyDic=[JsonStr objectFromJSONString];
//申明block返回的累
RespInfo* retobj = [[RespInfo alloc]init];
//判断返回是否成功
NSString *IsOrNoSuccess=[MyDic objectForKey:@"Success"];
if ([IsOrNoSuccess intValue]==1) {
retobj.mBSuccess=YES;
//这里判断返回类型,如果为0不去取mObject的值,否则要取
int JudgeInt=[[MyDic objectForKey:@"ControlType"]intValue];
if (JudgeInt==0) {//只取列表值 NSString *DataListArrayStr=[MyDic objectForKey:@"Rows"];
retobj.listData=[DataListArrayStr objectFromJSONString]; }
else if(JudgeInt==1)//字取附加字典值
{
NSString *addicationDic=[MyDic objectForKey:@"AddicationDictionary"];
retobj.mObjectDictionary=[addicationDic objectFromJSONString];
}
else if(JudgeInt==2)//两个都取
{ NSString *DataListArrayStr=[MyDic objectForKey:@"Rows"];
retobj.listData=[DataListArrayStr objectFromJSONString]; NSString *addicationDic=[MyDic objectForKey:@"AddicationDictionary"];
retobj.mObjectDictionary=[addicationDic objectFromJSONString];
}
//返回的公用提示消息
NSString *AlertInfo=[MyDic objectForKey:@"Msg"];
retobj.mMsg=AlertInfo; }
else
{ retobj.mBSuccess=NO; NSString *AlertInfo=[MyDic objectForKey:@"Msg"];
retobj.mMsg=AlertInfo; } callback(retobj);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { RespInfo* retobj = [[RespInfo alloc]init];
retobj.mBSuccess=NO; retobj.mMsg=error.domain; }]; }
}]; } // @end

block返回的类

#pragma mark - RespInfo  外层封装数据
@interface RespInfo : AFDataDefine
@property (nonatomic,strong) NSArray *listData; //列表数据
@property(nonatomic,strong)NSDictionary *mObjectDictionary;//附带字典
@property (nonatomic,strong) NSString* mMsg; //提示消息
@property (nonatomic,assign) BOOL mBSuccess; //返回状态,成功失败,判断这个 +(RespInfo *)infoWithError:(NSError *)error;
+(RespInfo *)infoWithErrorMessage:(NSString *)errMsg; @end

调用

 #import "ViewController.h"
#import "APIClient.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSUUID *iD=[[NSUUID alloc]init];
NSString *UUIDvALUE=[iD UUIDString]; NSDictionary *Dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"",@"dataType", nil]; [[APIClient sharedClient] getUrl:@"/Mobile/Jmfww.asmx/JmfwwCommunity" parameters:Dic call:^(RespInfo *info) {
if ([info mBSuccess]) { }
}]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

本人创业做的一款androidApp, 下载量已经有2000多万,各种当前热门的网络手机奖励红包全部集成,另外还有热门电影和淘宝高额优惠券!很适合各类型的用户。

 AFNetworking 3.1

上一篇:网络爬虫(java)


下一篇:eclipse安装svn和maven插件以及m2e-extras