1.AFN http get
AFHTTPRequestOperationManager
内部包装了NSURLConnection
- (void)afnHttpGet{
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"username" : @"solozyx",@"pwd" : @"solozyx"};
[mgr GET:@"http://www.example.com:8080/login"
parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"请求成功---%@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
}
// {success = "请求成功";}
AFHTTPSessionManager
内部包装了NSURLSession
- (void)afnHttpGet2{
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
NSDictionary *params = @{@"username" : @"solozyx",@"pwd" : @"solozyx"};
[mgr GET:@"http://www.example.com:8080/login"
parameters:params
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"请求成功---%@", responseObject);
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
}
// {success = "请求成功";}
2.AFN http post
- (void)afnHttpPost{
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"username" : @"solozyx",@"pwd" : @"solozyx"};
[mgr POST:@"http://www.example.com:8080/login"
parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"请求成功---%@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
}
// {success = "请求成功";}
3.AFN http uoload
- (void)afnHttpUpload{
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
NSDictionary *params = @{@"username" : @"123"};
[mgr POST:@"http://120.25.226.186:32812/upload"
parameters:params
constructingBodyWithBlock:^(id formData) {
// 在这个block中设置需要上传的文件
NSData *data = [NSData dataWithContentsOfFile:@"/Users/admin/Desktop/afnuploadtest.png"];
[formData appendPartWithFileData:data // 文件数据
name:@"file" // 服务端接收文件的参数
fileName:@"test.png" // 上传到服务端保存的文件名
mimeType:@"image/png"]; // 上传文件类型
/*
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/admin/Desktop/afnuploadtest.png"]
name:@"file"
fileName:@"xxx.png"
mimeType:@"image/png"
error:nil];
*/
/*
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/admin/Desktop/afnuploadtest.png"]
name:@"file"
error:nil];
*/
}
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"上传成功---%@", responseObject);
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"上传成功---%@", error);
}
];
}
// {success = "请求成功";}
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
[mgr uploadTaskWithRequest:(NSURLRequest *)
fromData:(NSData *)
progress:(NSProgress *__autoreleasing *)
completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
//code
}];
afn download
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
mgr downloadTaskWithRequest:<#(NSURLRequest *)#>
progress:<#(NSProgress *__autoreleasing *)#>
destination:<#^NSURL *(NSURL *targetPath, NSURLResponse *response)destination#>
completionHandler:<#^(NSURLResponse *response, NSURL *filePath, NSError *error)completionHandler#>
4.AFN默认解析http响应数据格式是JSON,设置解析数据格式为XML
- (void)afnHttpResponseXml{
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// responseSerializer 用来解析服务器返回的数据
// 告诉AFN,以XML形式解析服务器返回的数据
mgr.responseSerializer = [AFXMLParserResponseSerializer serializer];
NSDictionary *params = @{@"username" : @"solozyx",@"pwd" : @"solozyx",@"type" : @"XML"};
[mgr GET:@"http://www.example.com:8080/login"
parameters:params
success:^(NSURLSessionDataTask *task, NSXMLParser *parser) {
NSLog(@"请求成功---%@", parser);
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
}
// 请求成功---
5.AFN设置使用http响应二进制数据格式
- (void)afnHttpResponseOriginal{
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// 直接使用“服务器本来返回的数据”,不做任何解析
mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
//[AFJSONResponseSerializer serializer]; // 解析服务器返回的JSON数据
//[AFXMLParserResponseSerializer serializer]; // 解析服务器返回的XML数据
[mgr GET:@"http://www.example.com:8080/resources/images/minion_02.png"
parameters:nil
success:^(NSURLSessionDataTask *task, NSData *responseObject) {
NSLog(@"请求成功---%zd", responseObject.length);
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
}
// 请求成功---43852
6.AFN network monitoring
#pragma mark - AFNetworkReachability
- (void)afnNetworkMonitoring{
// 开始网络监控
AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
[mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"当前的网络状态---%zd", status);
}];
[mgr startMonitoring];
// 拿到当前网络状态
// mgr.networkReachabilityStatus;
}
//当前的网络状态---2
7.Apple 官方 Reachability
/** 监控网络 */
@property (nonatomic, strong) Reachability *reachability;
#pragma mark - Reachability internet notifier
- (void)appleMonitoring{
// 监听通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(getNetworkStatus)
name:kReachabilityChangedNotification
object:nil];
// 开始监控网络
self.reachability = [Reachability reachabilityForInternetConnection];
[self.reachability startNotifier];
}
- (void)getNetworkStatus{
if ([Reachability reachabilityForLocalWiFi].currentReachabilityStatus != NotReachable) {
NSLog(@"是wifi");
} else if ([Reachability reachabilityForInternetConnection].currentReachabilityStatus != NotReachable) {
NSLog(@"是手机自带网络");
} else {
NSLog(@"网络有问题");
}
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.reachability stopNotifier];
self.reachability = nil;
}
// Reachability Flag Status: -- ------- networkStatusForFlags
// 网络有问题
// Reachability Flag Status: -R ------d localWiFiStatusForFlags
// 是wifi