AFNetWorking的使用

本人是初学iOS菜鸟一个,对于AFNetworing的框架很是记不住,在此总结一下,防止以后忘记。

AFNetWorking的官网地址:https://github.com/AFNetworking/AFNetworking

// 1.需要加入SystemConfiguration.framework、MobileCoreServices.framework、Security.framework三个框架

// 2.消除警告在pch文件中添加#import #import

// 3.引入AFNetworking框架#import "AFNetworking.h"

// 4.下面是详细代码,包括:简单网络请求、图片请求、JSON数据请求、XML数据请求、流媒体播放功能


Get请求:

//普通网络请求

NSURL*url = [NSURLURLWithString:@"http://192.168.198.125/admin/index.php?m=interface&a=companies"];

NSURLRequest*request = [NSURLRequestrequestWithURL:url];

//准备AFHTTPRequestOperation

AFHTTPRequestOperation*operation = [[AFHTTPRequestOperationalloc]initWithRequest:request];

[operationsetCompletionBlockWithSuccess:^(AFHTTPRequestOperation*operation,idresponseObject) {

NSArray*array = operation.responseString;

NSLog(@"%@", array);

}failure:^(AFHTTPRequestOperation*operation,NSError*error) {

NSLog(@"Failure: %@", [errorlocalizedDescription]);

}];

//开始获取数据

[operationstart];


XML数据网络请求

NSURLRequest*request2 = [NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://api.flickr.com/services/rest/?method=flickr.groups.browse&api_key=b6300e17ad3c506e706cb0072175d047&cat_id=34427469792@N01&format=rest"]];

//准备AFXMLRequestOperation

AFXMLRequestOperation*operation2 = [AFXMLRequestOperationXMLParserRequestOperationWithRequest:request2success:^(NSURLRequest*request,NSHTTPURLResponse*response,NSXMLParser*XMLParser) {

//获取成功

XMLParser.delegate=self;

[XMLParserparse];

}failure:^(NSURLRequest*request,NSHTTPURLResponse*response,NSError*error,NSXMLParser*XMLParser) {

//获取失败

}];

//开始获取数据

[operation2start];


流媒体数组获取

//准备NSURLRequest

NSURLRequest*request4 = [NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://localhost:8080/encode"]];

//准备AFHTTPRequestOperation

AFHTTPRequestOperation*operation4 = [[AFHTTPRequestOperationalloc]initWithRequest:request4];

//设置inputStrem

operation4.inputStream= [NSInputStreaminputStreamWithFileAtPath:[[NSBundlemainBundle]pathForResource:@"large-image"ofType:@"tiff"]];

//设置outputStrem

operation4.outputStream= [NSOutputStreamoutputStreamToMemory];

[operation4start];


添加上传图片代码

//将图片转为NSData形式

NSData*imageData =UIImagePNGRepresentation(_thumbnailView.image);

//要上传的地址

NSURL*url = [NSURLURLWithString:[NSStringstringWithFormat:@"http://192.168.198.125/index.php?m=Interface&a=aptitude"]];

//初始化AFHttpClient对象

AFHTTPClient*client = [[AFHTTPClientalloc]initWithBaseURL:url];

//初始化NSURLRequest对象

NSURLRequest*request = [clientmultipartFormRequestWithMethod:@"POST"//发送请求的方式,需设置为POST方式

path:Nil//路径,可为空

parameters:@{

@"tel":@"13900000000",

@"tenantid":kServerPort,

@"aptitude":_aptitudeNameText.text

}//参数设置

constructingBodyWithBlock:^(id formData) {

//设置上图片的数据

[formDataappendPartWithFileData:imageData

name:@"aptitude_logo"//图片参数的名称

fileName:@"aptitude_logo.png"//上传的图片名称,可与参数名称不一致

mimeType:@"image/png"];//图片类型

}];

//初始化AFHTTPRequestOperation对象

AFHTTPRequestOperation*operation = [[AFHTTPRequestOperationalloc]initWithRequest:request];

//处理上传数据的结果的代码块

[operationsetCompletionBlockWithSuccess:^(AFHTTPRequestOperation*operation,idresponseObject) {

//上传成功,接收返回来的结果

NSLog(@"%@", [[NSStringalloc]initWithData:responseObjectencoding:NSUTF8StringEncoding]);

}failure:^(AFHTTPRequestOperation*operation,NSError*error) {

//上传失败,打印错误信息

NSLog(@"错误信息:%@", [errorlocalizedDescription]);

}];

//开始上传

[client.operationQueueaddOperation:operation];


注:你开发应用的时候,会经常使用到很多第三方开源类库,当类库更新的时候,你又必须重新下载然后手工导入是比较麻烦的,所以在这里给大家介绍一个新的类库管理工具CocoPods。当第三方类库有更新的时候在CocoPods中只需要一句命令就可解决上面的麻烦,当然最重要的一点就是你要正确的设置CocoPods(绝大多数有名得开源的类库都支持CocoPods),所以做开发经常为第三方类库更新发愁的骚年们,掌握CocoPods势在必行!!!

CocoPods教程附上:http://code4app.com/article/cocoapods-install-usage

你可能感兴趣的:(AFNetWorking的使用)