简介:
AFHTTPSessionManager
- AFHTTPSessionManager内部包装了NSURLSession
- 是AFN中最重要的对象之一
- 封装了HTTP请求的常见处理
- GET\POST请求
- 解析服务器的响应数据
- 创建
AFHTTPSessionManager*mgr= [AFHTTPSessionManagermanager];
AFHTTPRequestOperationManager
- AFHTTPRequestOperationManager内部包装了NSURLConnection
- 创建
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
GET\POST请求
//AFHTTPRequestOperationManager内部包装了NSURLConnection
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{
@"username" : @"520it",
@"pwd" : @"520it"
};
[mgr GET:@"http://120.25.226.186:32812/login" parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"请求成功---%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
// 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);
}];
// AFHTTPRequestOperationManager内部包装了NSURLConnection
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{
@"username" : @"520it",
@"pwd" : @"520it"
};
[mgr POST:@"http://120.25.226.186:32812/login" parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"请求成功---%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
上传文件
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
[mgr POST:@"http://120.25.226.186:32812/upload" parameters:@{@"username" : @"123"}
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// 在这个block中设置需要上传的文件
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/xiaomage/Desktop/placeholder.png"] name:@"file" error:nil];
} success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"-------%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];
解析服务器返回的数据
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// responseSerializer 用来解析服务器返回的数据
// 告诉AFN,以XML形式解析服务器返回的数据
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) {
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// 直接使用“服务器本来返回的数据”,不做任何解析
mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
// [AFJSONResponseSerializer serializer]; // 解析服务器返回的JSON数据
// [AFXMLParserResponseSerializer serializer]; // 解析服务器返回的XML数据
[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);
}];
网络状态监控
// 开始网络监控
AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
[mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"-----当前的网络状态---%zd", status);
}];
[mgr startMonitoring];
// 拿到当前网络状态
// mgr.networkReachabilityStatus;
// AFNetworkReachabilityStatusUnknown = -1,
// AFNetworkReachabilityStatusNotReachable = 0,
// AFNetworkReachabilityStatusReachableViaWWAN = 1, //自带网络
// AFNetworkReachabilityStatusReachableViaWiFi = 2, //WiFi
网友评论