#import "ViewController.h"
#import "Reachability.h"//苹果的
#import <AFNetworking.h>
@interface ViewController ()<NSXMLParserDelegate>
/** 苹果的监控网络 */
@property (nonatomic, strong) Reachability *reachability;
@end
@implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
// [self appleMonitoring];
//从web服务器加载数据
NSString *str = @"http://xxx.xml";
//1简历NSURL
NSURL *url = [NSURL URLWithString:str];
//2建立NSURLRequest
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:2.0f];
//3建立NSURLConnect的同步方法加载数据
NSURLResponse *response = nil;
NSError *error = nil;
//同步加载数据
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//1实例化解析器,传入要解析的数据
NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
//2 设置代理
[parser setDelegate:self];
//3解析器解析,这个方法会调用代理里面的方法。
[parser parse];
NSLog(@"%@",data);
}
-(void)appleMonitoring
{
// 监听通知
[[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(@"网络有问题");
}
}
-(void)afnMonitoring
{
// 开始网络监控
AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
[mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"-----当前的网络状态---%zd", status);
}];
[mgr startMonitoring];
// 拿到当前网络状态
// mgr.networkReachabilityStatus;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// 直接使用“服务器本来返回的数据”,不做任何解析
mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
// [AFJSONResponseSerializer serializer]; // 解析服务器返回的JSON数据
// [AFXMLParserResponseSerializer serializer]; // 解析服务器返回的XML数据
[mgr GET:@"http://xxx/test.png" parameters:nil success:^(NSURLSessionDataTask *task, NSData *responseObject) {
NSLog(@"请求成功---%zd", responseObject.length);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
}
-(void)xml
{
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// responseSerializer 用来解析服务器返回的数据
// 告诉AFN,以XML形式解析服务器返回的数据
mgr.responseSerializer = [AFXMLParserResponseSerializer serializer];
NSDictionary *params = @{
@"username" : @"xxx",
@"pwd" : @"xxx",
@"type" : @"XML"
};
[mgr GET:@"http://xxxx/login" parameters:params success:^(NSURLSessionDataTask *task, NSXMLParser *parser) {
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
}
-(void)upload
{
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
[mgr POST:@"http://xxxx/upload" parameters:@{@"username" : @"123"}
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// 在这个block中设置需要上传的文件
// NSData *data = [NSData dataWithContentsOfFile:@"/Users/mac/Desktop/xxxxpng"];
// [formData appendPartWithFileData:data name:@"file" fileName:@"test.png" mimeType:@"image/png"];
// [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/mac/Desktop/xxxx.png"] name:@"file" fileName:@"xxx.png" mimeType:@"image/png" error:nil];
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/mac/Desktop/placeholder.png"] name:@"file" error:nil];
} success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"-------%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];
}
-(void)get2
{
// AFHTTPSessionManager内部包装了NSURLSession
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
NSDictionary *params = @{
@"username" : @"xxxx",
@"pwd" : @"xxxx"
};
[mgr GET:@"http://xxxx/login" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"请求成功---%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
}
-(void)post
{
// AFHTTPRequestOperationManager内部包装了NSURLConnection
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{
@"username" : @"xxxx",
@"pwd" : @"xxxx"
};
[mgr POST:@"http://xxxx/login" parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"请求成功---%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
}
-(void)get
{
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{
@"username" : @"xxxx",
@"pwd" : @"xxxx"
};
[mgr GET:@"http://xxxx/login" parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"请求成功---%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
}
+(void)Upload:(NSString *)URLString parameters:(id)parameters uploadParam:(id)uploadParam success:(void (^)(id))success failure:(void (^)(NSError *))failure{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer.timeoutInterval = 10;
[manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:uploadParam name:@"" fileName:@"" mimeType:@""];
} success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
if (success) {
success(responseObject);
}
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}
@end