AFNetworking

AFNetworking网络请求库

导入AFNetworking.h头文件

//ASIHttpRequest,有三年多没更新,而且还是MRC,跟不上技术的发展了

//在ARC基本上全部都是使用AFNetworking:ARC下的一个网络请求库,还在不断的更新,使用简单方便

//发请求:全部都是异步请求,没有同步请求的方法

//使用manager可以发起GET/PUT/POST/PATCH...请求

//AFHTTPRequestOperationManager  -----> 2.0 NSURLConnection

//AFHTTPSessionManager      --------> 3.0 NSURLSession

//创建网络请求管理类

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

//AFNetworking:默认只支持三种响应头:application/json   text/json  text/JavaScript

//我们常用的还有一种:text/html,如果服务器需要该类型,则设置下面的参数

//(请求失败:在error中出现类似text/html这种类型的参数是再设置)

/*解析数据json

//响应头

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

[manager GET:@"http://localhost:8080/Login1/NewServlet?command=5" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

//处理请求成功的事件

//AFNetworking:返回的参数是json,AFNetworking会自动返回解析后的数据(NSArray,NSDictionary)

//不需要使用NSJSONSerialization解析json

if ([responseObject isKindOfClass:[NSData class]]) {

NSLog(@"data ----> %@",[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);

}else if ([responseObject isKindOfClass:[NSDictionary class]]){

NSLog(@"Dic -----> %@",responseObject);

}

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

//处理请求失败的事件

NSLog(@"----->%@",error);

}];

*/

/*

请求数据格式

AFHTTPRequestSerializer二进制格式

AFJSONRequestSerializer     JSON

AFPropertyListRequestSerializer    PList(是一种特殊的XML,解析起来相对容易)

响应数据格式

AFHTTPResponseSerializer 二进制格式

AFJSONResponseSerializer   JSON

AFXMLParserResponseSerializer     XML,只能返回XMLParser,还需要自己通过代理方法解析

AFXMLDocumentResponseSerializer (Mac OS X)

AFPropertyListResponseSerializer   PList

AFImageResponseSerializer          Image

AFCompoundResponseSerializer组合

*/


/*解析数据text/html

//设置响应数据的数据格式

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

//设置请求数据的数据格式

//manager.requestSerializer = [AFHTTPRequestSerializer serializer];

//(请求失败:在error  text/html

//manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

//请求的URL (NSString类型)

[manager GET:@"http://flash.weather.com.cn/wmaps/xml/china.xml" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {

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

if ([responseObject isKindOfClass:[NSData class]]) {

NSLog(@"data ----> %@",[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]);

}else if ([responseObject isKindOfClass:[NSDictionary class]]){

NSLog(@"Dic -----> %@",responseObject);

}

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

NSLog(@"----->%@",error);

}];

*/


/*解析数据text/html ,带参数请求

NSDictionary *parma =  @{@"command":@"5"};

//响应头

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

[manager POST:@"http://localhost:8080/Login1/NewServlet?command=5" parameters:parma constructingBodyWithBlock:^(id  _Nonnull formData) {

} progress:^(NSProgress * _Nonnull uploadProgress) {

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

if ([responseObject isKindOfClass:[NSData class]]) {

NSLog(@"data ----> %@",[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]);

}else if ([responseObject isKindOfClass:[NSDictionary class]]){

NSLog(@"Dic -----> %@",responseObject);

}

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

NSLog(@"----->%@",error);

}];

*/


//文件上传

//    [manager POST:@"请求路径" parameters:@"请求参数" constructingBodyWithBlock:^(id  _Nonnull formData) {

//上传文件

//        [formData appendPartWithFileData:@"data数据" name:@"参数名" fileName:@"文件名" mimeType:@"文件类型image/jpeg"];

//        [formData appendPartWithFormData:<#(nonnull NSData *)#> name:<#(nonnull NSString *)#>];

//    } progress:<#^(NSProgress * _Nonnull uploadProgress)uploadProgress#> success:<#^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)success#> failure:<#^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)failure#>

//检测网络状态

[manager.reachabilityManagersetReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

if(status == 0) {

NSLog(@"没有网络");

}elseif(status == 1){

NSLog(@"移动网络");

}elseif(status == 2){

NSLog(@"WiFi");

}

}];

[manager.reachabilityManager startMonitoring];

NSString *str = [[UIDevicecurrentDevice]name];

NSLog(@"%@",str);

[UIDevice currentDevice].batteryMonitoringEnabled = YES;

NSLog(@"%f",[UIDevice currentDevice].batteryLevel);

}

你可能感兴趣的:(AFNetworking)