FirstAFNetWorking
// ViewController.h
// FirstAFNetWorking
//
// Created by 张国锋 on 15/7/20.
// Copyright (c) 2015年 张国锋. All rights reserved.
// #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end //
// ViewController.m
// FirstAFNetWorking
//
// Created by 张国锋 on 15/7/20.
// Copyright (c) 2015年 张国锋. All rights reserved.
// #import "ViewController.h"
#import "AFNetworking.h"
#import "GDataXMLNode.h"
//json
#define kJSONUrlString @"http://www.baidu.com:8080/free/applications/limited?currency=rmb&page=1"
//xml的地址
#define kXMLUrlString @"http://wiapi.baidu.com/news/getlist4.0.php?pid=100234721&pc=20&pn=1&st=0" #define kJSONPostString @"http://网址/sjll/v1/homes/basic_data" #define kXMLPostString @"sadfasfdasdf" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
[self jsonGetRequest];
// [self xmlGetRequest];
// NSDictionary *dic=[NSDictionary dictionaryWithObjects:@[@"2"] forKeys:@[@"area_id"]];
// [self jsonPostRequestWith:dic];
// Do any additional setup after loading the view, typically from a nib.
} -(void)jsonGetRequest{
//对json数据的Get请求
AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
// manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"application/json"];//告诉manager,解析的类型是json数据
manager.responseSerializer=[[AFHTTPResponseSerializer alloc]init];
[manager GET:kJSONUrlString parameters:nil success:^(AFHTTPRequestOperation * operation, id responseObject) {//responseObject 接受的数据
NSLog(@"%ld",manager.operationQueue.operationCount);//请求完之后是出对列 为 0
if ([responseObject isKindOfClass:[NSData class]]) {
NSLog(@"%@",responseObject);
}else if([responseObject isKindOfClass:[NSDictionary class]]){
NSLog(@"%@",responseObject);
}
} failure:^(AFHTTPRequestOperation * operation, NSError * error) {
NSLog(@"wqrqwer%@",error);
}];
// manager.operationQueue.operationCount
NSLog(@"%ld",manager.operationQueue.operationCount);//manager.operationQueue.operationCount 队列里面有多少个请求
// [manager.operationQueue cancelAllOperations];//取消队列中所有的请求
}
-(void)xmlGetRequest{
AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
// manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"text/xml"];
//报错报3840 就是服务端的数据不是严格按照xml的格式书写的。
//xml解析xcode很难解析,我们使用GData
manager.responseSerializer=[[AFHTTPResponseSerializer alloc]init];//告诉AFNetWorking,我不需要你解析,我自己解析
[manager GET:kXMLUrlString parameters:nil success:^(AFHTTPRequestOperation * operation, id responseObject) {
// NSLog(@"%@",responseObject);
GDataXMLDocument *doc=[[GDataXMLDocument alloc]initWithData:responseObject encoding:NSUTF8StringEncoding error:nil];
NSString *path = @"/doc/focus/frame/title";
NSArray *titles=[doc nodesForXPath:path error:nil];
for (GDataXMLElement *title in titles) {
NSLog(@"%@",title.stringValue);
}
} failure:^(AFHTTPRequestOperation * operation, NSError * error) {
NSLog(@"%@",error);
}];
}
-(void)jsonPostRequestWith:(NSDictionary *)dic{
AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];//这是一个单列
manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"application/json"];
[manager POST:kJSONPostString parameters:dic success:^(AFHTTPRequestOperation * operation, id responseObject) {
NSLog(@"%@",responseObject);
} failure:^(AFHTTPRequestOperation * operation, NSError * error) {
NSLog(@"%@",error);
}];
}
-(void)xmlPostRequestWith:(NSDictionary *)dic{
AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
//manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"text/xml"];
manager.responseSerializer=[[AFHTTPResponseSerializer alloc]init];
[manager POST:kXMLPostString parameters:dic success:^(AFHTTPRequestOperation * operation, id responseObject) {
NSLog(@"%@",responseObject);
} failure:^(AFHTTPRequestOperation * operation, NSError * error) {
NSLog(@"%@",error);
}]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
Cocoa Pods 管理第三方库.
FirstAFNetWorking的更多相关文章
随机推荐
- 休假回来 更博-MySQL以月为单位的客户综合情况表_20161008
十一休假老家事比较多 未来得及更新 今起依旧更博- 生成一个以用户ID为单位,各月下单天次,各月买了几个产品,各月订单额 ,天次,,天次,,天次,NULL)) AS 9月天次 FROM ( SELEC ...
- php之配置redis
Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. 配置说明:https://www.cnblogs.com/lucky-man/p/8359110.html ph ...
- dubbo的扩展点重构
可扩展设计是框架要重点考虑的设计,因为它直接影响到框架的稳定性和功能的扩展,Dubbo扩展点重构.它在扩展性设计上踩过的坑,值得框架设计者借鉴学习. 第一步,微核心,插件式,平等对待第三方 即然要扩展 ...
- 在visualstudio中使用Qt
1. 说明 在此说明一下IDE跟封装的之间的关系,他们之间本质上来说没有关系,是可以多对对的关系. Qt开发是个比较泛的概念,Qt是由很多一系列类组成的整体,就像boost里面也有很多的类,而boo ...
- VMware设置桥接网络
VMware设置桥接网络 2011-12-30 08:57:04 分类: LINUX 一.桥接网络的基本原理 配置成桥接网络连接模式的虚拟机就当作主机所在以太网的一部分, 虚拟系统和宿主机器的 ...
- 《Java多线程编程核心技术》读后感(十一)
方法join的使用 在很多情况下,主线程创建并启动子线程,如果子线程中要进行大量的耗时运算,主线程往往将早于子线程结束之前结束.这时,如果主线程想等待子线程执行完之后再结束,比如子线程处理一个数据,主 ...
- Spring入门第七课
Bean之间的关系:继承和依赖. 继承Bean配置 Spring允许继承bean的配置,被继承的bean称为父bean,继承这个父bean的Bean称为子Bean. 子Bean从父Bean中继承配置, ...
- ASP.NET MVC (Umbraco)中如何设置网站超时自动退出
原文章请参考 https://edgewebware.com/2014/06/automatically-log-out-members-send-login-page-umbraco/ 在网站开发 ...
- 2. nmap扫描神器总结
-----------------nmap(选项)(参数)------------------O:激活操作探测: -P0:值进行扫描,不ping主机: -PT:是同TCP的ping: -sV:探测服务 ...
- ACM-ICPC2018北京网络赛 Saving Tang Monk II(bfs+优先队列)
题目1 : Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 <Journey to the West>(also < ...