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的更多相关文章
随机推荐
- [SHOI 2017] 分手是祝愿
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4872 [算法] 首先发现 , 对于一个开关 , 按下2次和没按是等价的 , 因此每个 ...
- 【LeetCode】312. Burst Balloons
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...
- UML统一建模语UML2和EnterpriseArchitect
其实前面的UML统一建模语言(一)所描述的都是UML1的内容,现在咱们聊一聊UML2. UML2.x完全建立在UML1.x基础之上,大多数的UML1.x模型在UML2.x中都可用.但UML2.x在结构 ...
- 【opencv学习笔记六】图像的ROI区域选择与复制
图像的数据量还是比较大的,对整张图片进行处理会影响我们的处理效率,因此常常只对图像中我们需要的部分进行处理,也就是感兴趣区域ROI.今天我们来看一下如何设置图像的感兴趣区域ROI.以及对ROI区域图像 ...
- jquery中对于批量deferred的处理
此代码仿照jquery源码中$.when()的实现 function test(i) { var dfd = $.Deferred(); if(i%2 == 0) { console.log(&quo ...
- 微信小程序开发之日期组件
一: wxml: <view class="navbarlift" style="background:#ffffff;padding:20rpx"> ...
- SetROP2
一个Windows API SetROP2(int nDrawMode)的使用 该函数的主要的作用是根据nDrawMode设置的方式重新设定绘图的方式,下面就不同的nDrawMode值具体解释绘图模式 ...
- POJ 2387 Til the Cows Come Home Dijkstra求最短路径
Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...
- 存储过程接收JSON格式数据
前端有可能一次性上传多笔记录,并使用JSON序列化. 现在在MS SQL Server 2016版本上,可以直接处理JSO数据. 如下面的前端序列化的数据: DECLARE @json_string ...
- 一些unity资源
雨凇解包 http://www.xuanyusong.com/archives/3618 http://www.cnblogs.com/lixiang-share/p/5840444.html u3d ...