get请求
- AFHTTPSessionManager内部包装了NSURLSession
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
NSDictionary *params = @{
@"username" : @"520it",
@"pwd" : @"520it"
};
[mgr GET:@"http://120.25.226.186:32812/login" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error);
}];
post请求
- AFHTTPSessionManager内部包装了NSURLSession
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
NSDictionary *params = @{
@"username" : @"520it",
@"pwd" : @"520it"
};
[mgr POST:@"http://120.25.226.186:32812/login" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error);
}];
文件上传
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
[mgr POST:@"http://120.25.226.186:32812/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// 在这个block中设置需要上传的文件
// 这个方法内部会自动生成上传的文件的fileName、mimeType
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/ahuan/Desktop/placeholder.png"] name:@"file" error:nil];
// [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/ahuan/Desktop/placeholder.png"] name:@"file" fileName:@"text.png" mimeType:@"image/png" error:nil];
// [formData appendPartWithFileData:[NSData dataWithContentsOfFile:@"/Users/ahuan/Desktop/placeholder.png"] name:@"file" fileName:@"text2.png" mimeType:@"image/png"];
// 注意这个方法看着跟前面三个很像,但是调用这个方法没法上传
// [formData appendPartWithFormData:[NSData dataWithContentsOfFile:@"/Users/ahuan/Desktop/placeholder.png"] name:@"file"];
} success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
nil;
}];
监控网络环境
ANNetworking
AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
[mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"----当前的网络状态----%zd", status);
}];
[mgr startMonitoring];
mgr.networkReachabilityStatus;
苹果自带的Reachability的框架
- 下载地址:https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
#import "ViewController.h"
#import "Reachability.h"
#import <AFNetworking.h>
@interface ViewController ()
@property (nonatomic, strong) Reachability *reachability;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNetworkStatus) name:kReachabilityChangedNotification object:nil];
self.reachability = [Reachability reachabilityForInternetConnection];
[self.reachability startNotifier];
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.reachability stopNotifier];
self.reachability = nil;
}
- (void)getNetworkStatus{
if ([Reachability reachabilityForLocalWiFi].currentReachabilityStatus != NotReachable) {
NSLog(@"是wifi");
} else if ([Reachability reachabilityForInternetConnection].currentReachabilityStatus != NotReachable) {
NSLog(@"是手机自带网络");
} else {
NSLog(@"网络有问题");
}
}
@end
细节
AFHTTPSessionManager的responseSerializer属性
- 通过设置responseSerializer,可以设置把 服务器返回的数据解析成什么类型
[AFHTTPResponseSerializer serializer];
[AFJSONResponseSerializer serializer];
- 解析服务器返回的JSON数据(如果不设置,默认返回这个格式)
[AFXMLParserResponseSerializer serializer];
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
mgr.responseSerializer = [AFXMLParserResponseSerializer serializer];
NSDictionary *params = @{
@"username" : @"520it",
@"pwd" : @"520it",
@"type" : @"XML"
};
[mgr GET:@"http://120.25.226.186:32812/login" parameters:params success:^(NSURLSessionDataTask *task, NSXMLParser *parser) {
parser.delegate = self;
[parser parse];
NSLog(@"-----请求成功%@", parser);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"-----请求成功%@", error);
}];
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// 直接使用“服务器本来返回的数据”,不做任何解析
mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
[mgr GET:@"http://120.25.226.186:32812/resources/images/minion_02.png" parameters:nil success:^(NSURLSessionDataTask *task, NSData *responseObject) { NSLog(@"请求成功---%zd", responseObject.length);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"请求失败---%@", error); }];