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的更多相关文章
随机推荐
- ACM学习历程—HDU1716 排列2(dfs && set容器)
Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡片上的数字( ...
- hdu畅通工程
传送门 畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- win10系统下安装64位Oracle11g+LSQL Developer
LSQL Developer作为强大的Oracle编辑工具,却只支持32bit,本文提供在安装用LSQL Developer打开64bitOracle的操作方法 工具/原料 oracle11g安装包 ...
- SpringMVC之二:配置 Spring MVC
Servlet 3.0规范在2009年12月份就发布了,因此很有可能你会将应用部署到支持Servlet 3.0的Servlet容器之中,如tomcat7.0及以上.在Servlet 3 规范中,可以使 ...
- Jasper-template
ylbtech-Jasper: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtech. ...
- 排名Top 16的Java实用类库
(转载: http://www.hollischuang.com/archives/1606) github地址: https://github.com/liufeiSAP/javaStudy.git ...
- CF-835C
C. Star sky time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- 利用oracle session context 向oracle传值
有时候,我们在执行数据库请求时,需要向数据库传一些应用程序的上下文信息,比如当前应用的用户.举个场景,我们要通过触发器记录对某些关键表的修改日志,日志包括修改的表,字段,字段的值,修改的时间,当然非常 ...
- jmeter压力测试报告
XXX压力测试报告 时间:2015-08-04 测试人员:xxx 目录 XXX压力测试报告... 1 一 测试 ...
- 从头开始学Web开发—CSS_01
CSS导入使用及引用的两种方法: 外部引用 外部引用的方式,我们直接可以通过link标签来引用我们写好的一个CSS文件: 在link 标签中,我们通过rel = "stylesheet&qu ...