1:AFNetworking简称为AFN,是对NSURLConnection,NSURLSession的一层封装
2:使用:现在项目中导入AFNetworking,在头文件中包含,即
#import "AFNetworking.h"
下面我就直接贴代码了,里面有详细的解释
用get方法,解析json
<span style="font-size:18px;">-(void)getjson
{
//1.创建一个请求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
//声明一下:服务器返回的是json数据,默认返回的是json数据,所以不用声明
//mgr.responseSerializer = [AFJSONResponseSerializer serializer];
//responseObject 类型是NSDictionary或者是NSArray
//2.请求参数,
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"Service"] = @"adminCoach";
params[@"Loginname"] = @"admim";
params[@"Page"] = @"1";
//3.发送一个get请求
NSString *url = @"http://localhost:8080/Jiaxiao2/admin.php/Appadmin/index";
[mgr GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//请求成功时调用这个block
NSLog(@"请求成功-----%@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//请求失败时调用这个block
NSLog(@"请求失败");
}];
}
</span>
用get方法,解析XML
<span style="font-size:18px;">-(void)getXML
{
//1.创建一个请求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
//声明一下:服务器返回的是XML数据
//responseObject 类型是NSXMLParser
mgr.responseSerializer = [AFXMLParserResponseSerializer serializer];
//2.请求参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"username"] = @"123";
params[@"pwd"] = @"345";
params[@"type"] = @"XML";
//3.发送一个get请求,返回来的是NSXMLParser
NSString *url = @"http://localhost:8080/zhong/login";
[mgr GET:url parameters:params success:^(AFHTTPRequestOperation *operation, NSXMLParser *responseObject) {
//请求成功时调用这个block
NSLog(@"请求成功-----%@",responseObject);
//对NSXMLParser进行解析
//设置代理
// responseObject.delegate =self;
// [responseObject parse];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//请求失败时调用这个block
NSLog(@"请求失败");
}];
}
</span>
用get方法,解析data
<span style="font-size:18px;">-(void)getData
{
//1.创建一个请求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
//声明一下:服务器返回的是data数据,即不要对服务器返回的数据进行解析,直接返回data即可
//文件下载肯定是用这个
//responseObject 类型是NSData
mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
//2.请求参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"username"] = @"123";
params[@"pwd"] = @"345";
//3.发送一个get请求,返回来的是NSXMLParser
NSString *url = @"http://localhost:8080/zhong/login";
[mgr GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//请求成功时调用这个block
NSLog(@"请求成功-----%@",[responseObject class]);
// //返回的是xml时
// NSXMLParser *parser = [[NSXMLParser alloc]initWithData:responseObject];
// //返回的是json时
// NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//请求失败时调用这个block
NSLog(@"请求失败");
}];
}
</span>
用post方法,解析json
<span style="font-size:18px;">-(void)postjson
{
//1.创建一个请求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
//声明一下:服务器返回的是json数据,默认返回的是json数据,所以不用声明
//mgr.responseSerializer = [AFJSONResponseSerializer serializer];
//responseObject 类型是NSDictionary或者是NSArray
//2.请求参数,
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"Service"] = @"adminCoach";
params[@"Loginname"] = @"admim";
params[@"Page"] = @"1";
//3.发送一个POST请求
NSString *url = @"http://localhost:8080/Jiaxiao2/admin.php/Appadmin/index";
[mgr POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//请求成功时调用这个block
NSLog(@"请求成功-----%@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//请求失败时调用这个block
NSLog(@"请求失败");
}];
}
</span>
<span style="font-size:18px;">-(void)getSession
{
//AFHTTPSessionManager 对NSURLSession的封装
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
[mgr GET:@"" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];
}</span>