/**
* 实战1.通过AFNetworkReachabilityManager可以用来检测网络状态的变化
*/
- (void)netWorkMonitor{
//网络指示器
[UIApplicationsharedApplication].networkActivityIndicatorVisible =YES;
AFNetworkReachabilityManager *reachManager = [AFNetworkReachabilityManagersharedManager];
//开启网络检测管理
[reachManager startMonitoring];
//判断网络状态
[reachManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
/**
* AFNetworkReachabilityStatusUnknown = -1,
* AFNetworkReachabilityStatusNotReachable = 0,
* AFNetworkReachabilityStatusReachableViaWWAN = 1,
* AFNetworkReachabilityStatusReachableViaWiFi = 2,
*/
switch (status) {
caseAFNetworkReachabilityStatusUnknown:{
UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"网络状态"message:@"网络异常"preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *confirm = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {
NSLog(@"网络异常!");
}];
[alertController addAction:confirm];
[self presentViewController:alertController animated:YEScompletion:^{
}];
break;
}
caseAFNetworkReachabilityStatusNotReachable:{
UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"网络状态"message:@"网络没有连接"preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *confirm = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {
NSLog(@"网络没有连接!");
}];
[alertController addAction:confirm];
[self presentViewController:alertController animated:YEScompletion:^{
}];
break;
}
caseAFNetworkReachabilityStatusReachableViaWWAN:{
self.networkStatus.text =@"WAN连接";
break;
}
caseAFNetworkReachabilityStatusReachableViaWiFi:{
self.networkStatus.text =@"WiFi连接";
break;
}
}
}];
}
/**
* 实战2:通过AFHTTPSessionManager来请求JSON,XML和PLIST格式的服务器响应数据
*/
/**
* 数据:
{
"CBA": [
{
"name": "rose",
"age": "25"
},
{
"name": "jack",
"age": "23"
}
],
"NBA": [
{
"name": "tom",
"sex": "man"
},
{
"name": "jack",
"sex": "women"
}
]
}
*/
//json解析
- (void)jsonParse{
//创建请求的URL
NSString *urlStr =@"http://192.168.0.103:8080/theaddressbook/testController/doPost.do";
//创建会话管理员
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManagermanager];
//使用Json
sessionManager.responseSerializer = [AFJSONResponseSerializerserializer];
[sessionManager POST:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"%lld/%lld",downloadProgress.completedUnitCount,downloadProgress.totalUnitCount);
} success:^(NSURLSessionDataTask *_Nonnull task,id _Nullable responseObject) {
NSDictionary *dic = (NSDictionary *) responseObject;
NSArray *CBAs = dic[@"CBA"];
NSArray *NBAs = dic[@"NBA"];
NSLog(@"CBA如下:");
for (NSDictionary *cbain CBAs) {
NSLog(@"name = %@,age = %@",cba[@"name"],cba[@"age"]);
}
NSLog(@"NBA如下:");
for (NSDictionary *nbain NBAs) {
NSLog(@"name = %@,age = %@",nba[@"name"],nba[@"sex"]);
}
} failure:^(NSURLSessionDataTask *_Nullable task,NSError *_Nonnull error) {
NSLog(@"解析失败,%@",error);
}];
}
//2016-02-21 10:03:06.522 AFNetworking[1084:65887] 128/128
//2016-02-21 10:03:06.548 AFNetworking[1084:65856] CBA如下:
//2016-02-21 10:03:06.548 AFNetworking[1084:65856] name = rose,age = 25
//2016-02-21 10:03:06.548 AFNetworking[1084:65856] name = jack,age = 23
//2016-02-21 10:03:06.549 AFNetworking[1084:65856] NBA如下:
//2016-02-21 10:03:06.549 AFNetworking[1084:65856] name = tom,age = man
//2016-02-21 10:03:06.549 AFNetworking[1084:65856] name = jack,age = women
/**
* 文件名:personInfo.plist
* 数据:
*
http://www.apple.com/DTDs/PropertyList-1.0.dtd">
*/
//plist解析
- (void)plistParse{
NSString *urlStr =@"http://localhost:8080/theaddressbook/files/personInfo.plist";
//创建会话管理员
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManagermanager];
//plist序列化
sessionManager.responseSerializer = [AFPropertyListResponseSerializerserializer];
sessionManager.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"text/xml"];
[sessionManager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask *_Nonnull task,id _Nullable responseObject) {
NSDictionary *dic = (NSDictionary *)responseObject;
NSLog(@"username = %@,age = %@,address = %@",dic[@"username"],dic[@"age"],dic[@"address"]);
NSLog(@"解析成功了");
} failure:^(NSURLSessionDataTask *_Nullable task,NSError *_Nonnull error) {
NSLog(@"解析失败了 %@",error);
}];
}
//2016-02-21 11:57:06.911 AFNetworking[2792:149472] username = ljs,age = 25,address =广东揭阳
//2016-02-21 11:57:06.912 AFNetworking[2792:149472] 解析成功了
/**
* 文件名:personInfo.xml
* 数据:
原理:
这种解析方式是利用它的代理NSXMLParserDelegate实现的
第一个代理方法:开始处理xml数据,它会把整个xml遍历一遍,识别元素节点名称
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;
第二个代理方法:也就是得到文本节点里存储的信息数据
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
第三个代理方法:存储从第二个代理方法中获取到的信息
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
这就是解析的过程,在这个过程中会不停的重复的执行这三个代理方法,直到遍历完成
另外:
解析开始执行的方法
- (void)parserDidStartDocument:(NSXMLParser *)parser;
解析结束执行的方法
- (void)parserDidEndDocument:(NSXMLParser *)parser;
当出现解析错误的时候,会执行这个方法
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError;
*/
@interface ViewController () <NSXMLParserDelegate>
{
NSMutableString *element;
}
//xml解析
- (void)xmlParse{
//访问xml的url
NSString *urlStr =@"http://localhost:8080/theaddressbook/files/personInfo.xml";
//会话管理员
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManagermanager];
//设置xml解析
sessionManager.responseSerializer = [AFXMLParserResponseSerializerserializer];
[sessionManager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask *_Nonnull task,id _Nullable responseObject) {
NSXMLParser *XMLParser = (NSXMLParser *)responseObject;
[XMLParser setShouldProcessNamespaces:YES];
//解析XML数据要复杂许多,参考代理实现
XMLParser.delegate = self;
[XMLParser parse];
NSLog(@"解析成功了");
} failure:^(NSURLSessionDataTask *_Nullable task,NSError *_Nonnull error) {
NSLog(@"解析失败了 %@",error);
}];
}
//开始解析
- (void)parserDidStartDocument:(NSXMLParser *)parser{
NSLog(@"开始解析,做一些初始化!");
//获取文本节点中的数据,因为下面的方法要保存这里获取的数据,所以要定义一个全局变量(可修改的字符串)
element = [[NSMutableStringalloc]init];
}
//第一个代理方法:
//每当遇到元素节点的时候都会执行第一个代理方法,如果有属性节点,可以直接在这个方法中获取里面的值;
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
//判断是否是meeting
if ([elementName isEqualToString:@"meeting"]) {
//判断属性节点
if ([attributeDict objectForKey:@"addr"]) {
//获取属性节点中的值
NSString *addr=[attributeDict objectForKey:@"addr"];
NSLog(@"addr====%@",addr);
}
}
//判断member
if ([elementName isEqualToString:@"member"]) {
NSLog(@"member");
}
}
//第二个代理方法:
//每当遇到文本节点的时候都会执行第二个代理方法,获取文本节点中的值然后到第三个方法中进行区分。
//如果是换行符的话也会获取,因为换行符也是文本节点,不过当一个元素节点结束后的换行符是不会获取
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
//这里要赋值为空,目的是为了清空上一次的赋值
[element setString:@""];
[elementappendString:string];//string是获取到的文本节点的值,只要是文本节点都会获取(包括换行),然后到下个方法中进行判断区分
}
//第三个代理方法:
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"creator"]) {
NSLog(@"creator=%@",element);
}
if ([elementName isEqualToString:@"name"]) {
NSLog(@"name=%@",element);
}
if ([elementName isEqualToString:@"age"]) {
NSLog(@"age=%@",element);
}
}
//结束解析
- (void)parserDidEndDocument:(NSXMLParser *)parser{
NSLog(@"结束解析!");
}
//2016-02-21 14:02:15.378 AFNetworking[4636:250356] 开始解析,做一些初始化!
//2016-02-21 14:02:15.378 AFNetworking[4636:250356] addr====203
//2016-02-21 14:02:15.378 AFNetworking[4636:250356] creator=张一
//2016-02-21 14:02:15.378 AFNetworking[4636:250356] member
//2016-02-21 14:02:15.379 AFNetworking[4636:250356] name=张二
//2016-02-21 14:02:15.379 AFNetworking[4636:250356] age=20
//2016-02-21 14:02:15.379 AFNetworking[4636:250356] member
//2016-02-21 14:02:15.379 AFNetworking[4636:250356] name=张三
//2016-02-21 14:02:15.379 AFNetworking[4636:250356] age=21
//2016-02-21 14:02:15.379 AFNetworking[4636:250356] member
//2016-02-21 14:02:15.379 AFNetworking[4636:250356] name=张四
//2016-02-21 14:02:15.379 AFNetworking[4636:250356] age=22
//2016-02-21 14:02:15.380 AFNetworking[4636:250356] addr====204
//2016-02-21 14:02:15.380 AFNetworking[4636:250356] creator=李一
//2016-02-21 14:02:15.380 AFNetworking[4636:250356] member
//2016-02-21 14:02:15.380 AFNetworking[4636:250356] name=李二
//2016-02-21 14:02:15.380 AFNetworking[4636:250356] age=20
//2016-02-21 14:02:15.380 AFNetworking[4636:250356] member
//2016-02-21 14:02:15.380 AFNetworking[4636:250356] name=李三
//2016-02-21 14:02:15.408 AFNetworking[4636:250356] age=21
//2016-02-21 14:02:15.408 AFNetworking[4636:250356] member
//2016-02-21 14:02:15.408 AFNetworking[4636:250356] name=李四
//2016-02-21 14:02:15.409 AFNetworking[4636:250356] age=22
//2016-02-21 14:02:15.409 AFNetworking[4636:250356] 结束解析!
//2016-02-21 14:02:15.409 AFNetworking[4636:250356] 解析成功了