转自http://blog.csdn.net/codywangziham01/article/details/38088017
#import "MJViewController.h"
#import "AFNetworking.h"
@interfaceMJViewController ()
@end
@implementationMJViewController
/**
要使用常规的AFN网络访问
1. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
所有的网络请求,均有manager发起
2. 需要注意的是,默认提交请求的数据是二进制的,返回格式是JSON
1> 如果提交数据是JSON的,需要将请求格式设置为AFJSONRequestSerializer
2> 如果返回格式不是JSON的,
3. 请求格式
AFHTTPRequestSerializer 二进制格式
AFJSONRequestSerializer JSON
AFPropertyListRequestSerializer PList(是一种特殊的XML,解析起来相对容易)
4. 返回格式
AFHTTPResponseSerializer 二进制格式
AFJSONResponseSerializer JSON
AFXMLParserResponseSerializer XML,只能返回XMLParser,还需要自己通过代理方法解析
AFXMLDocumentResponseSerializer (Mac OS X)
AFPropertyListResponseSerializer PList
AFImageResponseSerializer Image
AFCompoundResponseSerializer 组合
*/
- (void)viewDidLoad
{
[superviewDidLoad];
[selfreach];
}
#pragma mark - 演练
#pragma mark - 检测网络连接
- (void)reach
{
/**
AFNetworkReachabilityStatusUnknown = -1, // 未知
AFNetworkReachabilityStatusNotReachable = 0, // 无连接
AFNetworkReachabilityStatusReachableViaWWAN = 1, // 3G 花钱
AFNetworkReachabilityStatusReachableViaWiFi = 2, // 局域网络,不花钱
*/
// 如果要检测网络状态的变化,必须用检测管理器的单例的startMonitoring
[[AFNetworkReachabilityManagersharedManager]startMonitoring];
// 检测网络连接的单例,网络变化时的回调方法
[[AFNetworkReachabilityManagersharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"%d", status);
}];
}
#pragma mark - Session 下载
- (void)sessionDownload
{
NSURLSessionConfiguration*config = [NSURLSessionConfigurationdefaultSessionConfiguration];
AFURLSessionManager*manager = [[AFURLSessionManageralloc]initWithSessionConfiguration:config];
NSString*urlString =@"http://localhost/itcast/videos/01.C语言-语法预览.mp4";
urlString = [urlStringstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL*url = [NSURLURLWithString:urlString];
NSURLRequest*request = [NSURLRequestrequestWithURL:url];
NSURLSessionDownloadTask*task = [managerdownloadTaskWithRequest:requestprogress:nildestination:^NSURL*(NSURL*targetPath,NSURLResponse*response) {
// 指定下载文件保存的路径
// NSLog(@"%@ %@", targetPath, response.suggestedFilename);
// 将下载文件保存在缓存路径中
NSString*cacheDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES)[0];
NSString*path = [cacheDirstringByAppendingPathComponent:response.suggestedFilename];
// URLWithString返回的是网络的URL,如果使用本地URL,需要注意
NSURL*fileURL1= [NSURLURLWithString:path];
NSURL*fileURL = [NSURLfileURLWithPath:path];
NSLog(@"== %@ |||| %@", fileURL1, fileURL);
returnfileURL;
}completionHandler:^(NSURLResponse*response,NSURL*filePath,NSError*error) {
NSLog(@"%@ %@", filePath, error);
}];
[taskresume];
}
#pragma mark - POST JSON
- (void)postJSON
{
AFHTTPRequestOperationManager*manager = [AFHTTPRequestOperationManagermanager];
NSDictionary*dict = @{@"name":@"zhangsan"};
NSDictionary*dict1= @{@"name":@"wangwu"};
NSArray*array = @[dict,dict1];
// 设置请求格式
manager.requestSerializer= [AFJSONRequestSerializerserializer];
// 设置返回格式
manager.responseSerializer= [AFHTTPResponseSerializerserializer];
[managerPOST:@"http://localhost/postjson.php"parameters:arraysuccess:^(AFHTTPRequestOperation*operation,idresponseObject) {
NSString*result = [[NSStringalloc]initWithData:responseObjectencoding:NSUTF8StringEncoding];
NSLog(@"%@", result);
}failure:^(AFHTTPRequestOperation*operation,NSError*error) {
}];
}
#pragma mark - 随机文件名上传
- (void)postUpload1
{
// 本地上传给服务器时,没有确定的URL,不好用MD5的方式处理
AFHTTPRequestOperationManager*manager = [AFHTTPRequestOperationManagermanager];
manager.responseSerializer= [AFHTTPResponseSerializerserializer];
[managerPOST:@"http://localhost/demo/upload.php"parameters:nilconstructingBodyWithBlock:^(id formData) {
NSURL*fileURL = [[NSBundlemainBundle]URLForResource:@"头像1.png"withExtension:nil];
// 要上传保存在服务器中的名称
// 使用时间来作为文件名 2014-04-30 14:20:57.png
// 让不同的用户信息,保存在不同目录中
NSDateFormatter*formatter = [[NSDateFormatteralloc]init];
// 设置日期格式
formatter.dateFormat=@"yyyy-MM-dd HH:mm:ss";
NSString*fileName = [formatterstringFromDate:[NSDatedate]];
[formDataappendPartWithFileURL:fileURLname:@"uploadFile"fileName:fileNamemimeType:@"image/png"error:NULL];
}success:^(AFHTTPRequestOperation*operation,idresponseObject) {
NSLog(@"OK");
}failure:^(AFHTTPRequestOperation*operation,NSError*error) {
NSLog(@"error");
}];
}
#pragma mark - POST上传
- (void)postUpload
{
AFHTTPRequestOperationManager*manager = [AFHTTPRequestOperationManagermanager];
// AFHTTPResponseSerializer就是正常的HTTP请求响应结果:NSData
// 当请求的返回数据不是JSON,XML,PList,UIImage之外,使用AFHTTPResponseSerializer
// 例如返回一个html,text...
//
// 实际上就是AFN没有对响应数据做任何处理的情况
manager.responseSerializer= [AFHTTPResponseSerializerserializer];
// formData是遵守了AFMultipartFormData的对象
[managerPOST:@"http://localhost/demo/upload.php"parameters:nilconstructingBodyWithBlock:^(id formData) {
// 将本地的文件上传至服务器
NSURL*fileURL = [[NSBundlemainBundle]URLForResource:@"头像1.png"withExtension:nil];
[formDataappendPartWithFileURL:fileURLname:@"uploadFile"error:NULL];
}success:^(AFHTTPRequestOperation*operation,idresponseObject) {
NSString*result = [[NSStringalloc]initWithData:responseObjectencoding:NSUTF8StringEncoding];
NSLog(@"完成 %@", result);
}failure:^(AFHTTPRequestOperation*operation,NSError*error) {
NSLog(@"错误 %@", error.localizedDescription);
}];
}
#pragma mark - JSON
- (void)XMLData
{
AFHTTPRequestOperationManager*manager = [AFHTTPRequestOperationManagermanager];
// 返回的数据格式是XML
manager.responseSerializer= [AFXMLParserResponseSerializerserializer];
NSDictionary*dict = @{@"format":@"xml"};
// 网络访问是异步的,回调是主线程的,因此程序员不用管在主线程更新UI的事情
[managerGET:@"http://localhost/videos.php"parameters:dictsuccess:^(AFHTTPRequestOperation*operation,idresponseObject) {
// 如果结果是XML,同样需要使用6个代理方法解析,或者使用第三方库
// 第三方库第三方框架,效率低,内存泄漏
NSLog(@"%@", responseObject);
}failure:^(AFHTTPRequestOperation*operation,NSError*error) {
NSLog(@"%@", error);
}];
}
#pragma mark - JSON
- (void)JSONData
{
AFHTTPRequestOperationManager*manager = [AFHTTPRequestOperationManagermanager];
// 原本需要拼接get访问URL ? & =
NSDictionary*dict = @{@"format":@"json"};
// 网络访问是异步的,回调是主线程的,因此程序员不用管在主线程更新UI的事情
[managerGET:@"http://localhost/videos.php"parameters:dictsuccess:^(AFHTTPRequestOperation*operation,idresponseObject) {
NSLog(@"%@", responseObject);
// 提问:NSURLConnection异步方法回调,是在子线程
// 得到回调之后,通常更新UI,是在主线程
NSLog(@"%@", [NSThreadcurrentThread]);
}failure:^(AFHTTPRequestOperation*operation,NSError*error) {
NSLog(@"%@", error);
}];
}
#pragma mark - POST登录
- (void)postLogin
{
AFHTTPRequestOperationManager*manager = [AFHTTPRequestOperationManagermanager];
// 原本需要拼接get访问URL ? & =
NSDictionary*dict = @{@"username":@"wangwu",@"password":@"wang"};
// 网络访问是异步的,回调是主线程的,因此程序员不用管在主线程更新UI的事情
[managerPOST:@"http://localhost/login.php"parameters:dictsuccess:^(AFHTTPRequestOperation*operation,idresponseObject) {
NSLog(@"%@", responseObject);
// 提问:NSURLConnection异步方法回调,是在子线程
// 得到回调之后,通常更新UI,是在主线程
NSLog(@"%@", [NSThreadcurrentThread]);
}failure:^(AFHTTPRequestOperation*operation,NSError*error) {
NSLog(@"%@", error);
}];
}
#pragma mark - GET登录
- (void)getLogin
{
AFHTTPRequestOperationManager*manager = [AFHTTPRequestOperationManagermanager];
// 原本需要拼接get访问URL ? & =
NSDictionary*dict = @{@"username":@"wangwu",@"password":@"wang"};
// 网络访问是异步的,回调是主线程的,因此程序员不用管在主线程更新UI的事情
[managerGET:@"http://localhost/login.php"parameters:dictsuccess:^(AFHTTPRequestOperation*operation,idresponseObject) {
NSLog(@"%@", responseObject);
// 提问:NSURLConnection异步方法回调,是在子线程
// 得到回调之后,通常更新UI,是在主线程
NSLog(@"%@", [NSThreadcurrentThread]);
}failure:^(AFHTTPRequestOperation*operation,NSError*error) {
NSLog(@"%@", error);
}];
}
@end