簡(jiǎn)介:
AFHTTPSessionManager
- AFHTTPSessionManager內(nèi)部包裝了NSURLSession
- 是AFN中最重要的對(duì)象之一
- 封裝了HTTP請(qǐng)求的常見(jiàn)處理
- GET\POST請(qǐng)求
- 解析服務(wù)器的響應(yīng)數(shù)據(jù)
- 創(chuàng)建
AFHTTPSessionManager*mgr= [AFHTTPSessionManagermanager];
AFHTTPRequestOperationManager
- AFHTTPRequestOperationManager內(nèi)部包裝了NSURLConnection
- 創(chuàng)建
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
GET\POST請(qǐng)求
//AFHTTPRequestOperationManager內(nèi)部包裝了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(@"請(qǐng)求成功---%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"請(qǐng)求失敗---%@", error);
}];
// AFHTTPSessionManager內(nèi)部包裝了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(@"請(qǐng)求成功---%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"請(qǐng)求失敗---%@", error);
}];
// AFHTTPRequestOperationManager內(nèi)部包裝了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(@"請(qǐng)求成功---%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"請(qǐng)求失敗---%@", error);
}];
上傳文件
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
[mgr POST:@"http://120.25.226.186:32812/upload" parameters:@{@"username" : @"123"}
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// 在這個(gè)block中設(shè)置需要上傳的文件
[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) {
}];
解析服務(wù)器返回的數(shù)據(jù)
- 默認(rèn)會(huì)解析JSON數(shù)據(jù)
- XML數(shù)據(jù)
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// responseSerializer 用來(lái)解析服務(wù)器返回的數(shù)據(jù)
// 告訴AFN炫狱,以XML形式解析服務(wù)器返回的數(shù)據(jù)
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(@"請(qǐng)求失敗---%@", error);
}];
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// 直接使用“服務(wù)器本來(lái)返回的數(shù)據(jù)”,不做任何解析
mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
// [AFJSONResponseSerializer serializer]; // 解析服務(wù)器返回的JSON數(shù)據(jù)
// [AFXMLParserResponseSerializer serializer]; // 解析服務(wù)器返回的XML數(shù)據(jù)
[mgr GET:@"http://120.25.226.186:32812/resources/images/minion_02.png" parameters:nil success:^(NSURLSessionDataTask *task, NSData *responseObject) {
NSLog(@"請(qǐng)求成功---%zd", responseObject.length);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"請(qǐng)求失敗---%@", error);
}];
網(wǎng)絡(luò)狀態(tài)監(jiān)控
// 開(kāi)始網(wǎng)絡(luò)監(jiān)控
AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
[mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"-----當(dāng)前的網(wǎng)絡(luò)狀態(tài)---%zd", status);
}];
[mgr startMonitoring];
// 拿到當(dāng)前網(wǎng)絡(luò)狀態(tài)
// mgr.networkReachabilityStatus;
// AFNetworkReachabilityStatusUnknown = -1,
// AFNetworkReachabilityStatusNotReachable = 0,
// AFNetworkReachabilityStatusReachableViaWWAN = 1, //自帶網(wǎng)絡(luò)
// AFNetworkReachabilityStatusReachableViaWiFi = 2, //WiFi