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的更多相关文章
随机推荐
- BZOJ_4025_二分图_线段树按时间分治+并查集
BZOJ_4025_二分图_线段树按时间分治+并查集 Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这么简 ...
- Node初学者入门,一本全面的NodeJS教程
作者: Manuel Kiessling 翻译: goddyzhao & GrayZhang & MondayChen 关于 本书致力于教会你如何用Node.js来开发应用,过程中会 ...
- bzoj1072Perm——状压DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1072 数字串最多只有10位,所以考虑用状压: 压缩的状态是哪些数字被用过,这样可以从一种状态 ...
- 如何为现有控件的DependencyProperty添加Value Changed事件?
主要是利用DependencyPropertyDescriptor 的AddValueChanged 方法, 比如下面的例子为DataGridColumn的VisibilityPr ...
- Spring读取加密属性文件处理--待整理
引言:Spring框架俨然已经是目前Java WEB项目开发的一个宠儿,更有人将Spring, Struts,和Hibernage称之为Java WEB项目开发的3件利器.Spring的依赖.注入.A ...
- javascript getAttribute
var nodes = document.getElementsByTagName("script"); var node = nodes[nodes.length - 1]; v ...
- fastIO模板
freadIO整理 namespace fastIO{ #define BUF_SIZE 100000 ; inline char nc() { static char buf[BUF_SIZE],* ...
- (I/O完成端口中的)995错误
在windows下,可能会出现995的错误,msdn对该错误的解释为: The I/O operation has been aborted because of either a thread ex ...
- HeapCreate
为进程创建新堆,请求分配虚拟内存分页,函数原型如下: HANDLE HeapCreate( DWORD flOptions, SIZE_T dwInitialSize, SIZE_T dwMaximu ...
- 性能压测,SQL查询异常
早上测试对性能压测,发现取sequence服务大量超时报错,查询线上的监控SQL: 大量这个查询,我在DeviceID和Isdelete上建有复合索引,应该很快,而且我测试了一下,取值,执行效率很高, ...