//  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的更多相关文章

随机推荐

  1. 如何理解 Spring 注入

    先看一段代码 假设你编写了两个类,一个是人(Person),一个是手机(Mobile). 人有时候需要用手机打电话,需要用到手机的dialUp方法. 传统的写法是这样: Java code publi ...

  2. 洛谷 P4336 黑暗前的幻想乡 —— 容斥+矩阵树定理

    题目:https://www.luogu.org/problemnew/show/P4336 当作考试题了,然而没想出来,呵呵. 其实不是二分图完美匹配方案数,而是矩阵树定理+容斥... 就是先放上所 ...

  3. 开源LTE代码分析

    跟踪了一个在将开源组织-OpenLTE(将4G通信网络LTE开源),现将自己梳理整理的一些文档Post出来,请有相同兴趣的朋友指点: 一,系统介绍 OpenLTE是一位Mot的工程师在12年发起的一个 ...

  4. Jenkins Email Extension Plugin 邮件插件

    1:系统管理-管理插件-可选插件  搜索Email 可列出Email Extension Plugin插件 2:选择相应的插件点  下载并安装之后重启,等待 3:安装完后,自己去重启tomcat,先s ...

  5. Hibernate中注解的开发

    转自:https://blog.csdn.net/liujiahan629629/article/details/22335563 在利用注解开发数据库持久层以前,需要学习一个规范JPA(JavaPe ...

  6. 基于keepalived的nginx高可用

    #nginx,keepalived安装略过 MASTER 节点配置文件(192.168.1.11) vi /etc/keepalived/keepalived.conf global_defs { # ...

  7. minihttp安装配置ssl和c语言实现cgi

    概述:参考了大牛们的方法,结合自己的环境做了修改,主要是讲:minihttp安装配置ssl和c语言实现cgi接收字符串并且保存系统环境:centos6.5 开发版 依赖软件包: mini_httpd- ...

  8. select2的远程加载非分页实例

    $("#c01-select").select2({ ajax: { url: "data.json", dataType: 'json', delay: 25 ...

  9. Windows对象、句柄与MFC对象

    windowsmfc编程c 目录(?)[-] Windows对象句柄与MFC对象 windows对象 句柄 mfc对象 杂谈 Windows对象.句柄与MFC对象 (2009-04-28 22:11: ...

  10. ACM-ICPC2018焦作网络赛 Participate in E-sports(大数开方)

    Participate in E-sports 11.44% 1000ms 65536K   Jessie and Justin want to participate in e-sports. E- ...