AFNetworking框架的使用

AFNetworking框架的使用,第1张

概述AFNetworking框架的使用

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

#import "VIEwController.h"#import "AFNetworking.h"@interface VIEwController ()@end@implementation VIEwController- (voID)vIEwDIDLoad {    [super vIEwDIDLoad];}-(voID)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{       [self sendGet];//    [self sendPost];//    [self upLoad];//    [self downLoad];       //    默认就是异步的请求!!}/** *  get 请求 */- (voID) sendGet{    AFhttpRequestoperationManager * mamaner=[AFhttpRequestoperationManager manager];//单例        //设置解析返回的数据的类型(默认就是解析Json的)(可以设置,有三种)//    mamaner.responseSerializer=[AFhttpResponseSerializer serializer];//不管返回什么样的数据,统一解析成二进制数据//    mamaner.responseSerializer = [AFXMLParserResponseSerializer serializer];//返回的是xml的,使用这个//    mamaner.responseSerializer = [AFJsONResponseSerializer serializer];//默认的        //get请求两种写法    //(1)写法一    Nsstring * [email protected]"http://192.168.2.162/logo.PHP?username=jereh&pwd=123";    [mamaner GET:url parameters:nil success:^(AFhttpRequestoperation *operation,ID responSEObject) {                NSLog(@"%@",responSEObject);            } failure:^(AFhttpRequestoperation *operation,NSError *error) {        NSLog(@"%@",error);    }];        //(2)写法二,类似post的写法//    Nsstring * [email protected]"http://192.168.2.162/logo.PHP";//    NSDictionary * [email protected]{@"username":@"jereh",@"pwd":@"123"};//    [mamaner GET:url parameters:dic success:^(AFhttpRequestoperation *operation,ID responSEObject) {//        //        NSLog(@"%@",responSEObject);//        //    } failure:^(AFhttpRequestoperation *operation,NSError *error) {//        NSLog(@"%@",error);//    }];    }/** *  post 请求 */- (voID) sendPost{        AFhttpRequestoperationManager * mamaner=[AFhttpRequestoperationManager manager];        //    mamaner.responseSerializer=[AFhttpResponseSerializer serializer];                Nsstring * [email protected]"http://192.168.2.162/loginPost";    NSDictionary * [email protected]{@"username":@"jereh",@"pwd":@"123"};        [mamaner POST:url parameters:dic success:^(AFhttpRequestoperation *operation,responSEObject);            } failure:^(AFhttpRequestoperation *operation,error);    }];     }/** *  post 请求(上传,使用post) */- (voID) upLoad{    AFhttpRequestoperationManager * mamaner=[AFhttpRequestoperationManager manager];    mamaner.responseSerializer=[AFhttpResponseSerializer serializer];        Nsstring * [email protected]"http://192.168.2.162/upload.PHP";        [mamaner POST:url parameters:nil constructingBodyWithBlock:^(ID<AFMultipartFormData> formData) {                NSURL * url=[[NSBundle mainBundle] URLForResource:@"exclusive_Title_icon.png" withExtension:nil];        [formData appendPartWithfileURL:url name:@"file" filename:@"jereh.png" mimeType:@"image/png" error:nil];                    } success:^(AFhttpRequestoperation *operation,ID responSEObject) {                Nsstring * str=[[Nsstring alloc] initWithData:responSEObject enCoding:NSUTF8StringEnCoding];        NSLog(@"%@",str);            } failure:^(AFhttpRequestoperation *operation,error);    }];        }/** *  post 请求(下载,get请求) */- (voID) downLoad{        //(0)创建manager对象    NSURLSessionConfiguration * config=[NSURLSessionConfiguration defaultSessionConfiguration];    AFURLSessionManager * manager=[[AFURLSessionManager alloc] initWithSessionConfiguration:config];            //(1)监控下载进度    [manager setDownloadTaskDIDWriteDataBlock:^(NSURLSession *session,NSURLSessionDownloadTask *downloadTask,int64_t bytesWritten,int64_t totalBytesWritten,int64_t totalBytesExpectedToWrite) {        //注意当前线程是子线程,需要返回主线程刷新数据        CGfloat progress=totalBytesWritten*1.0/totalBytesExpectedToWrite;//写入的比上总共的        dispatch_sync(dispatch_get_main_queue(),^{            self.progress.progress=progress;        });    }];        //(2)请求    NSURLRequest * request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.2.162/test.rar"]];    //注意下边的方法有返回值,block也有一个返回值    NSURLSessionDownloadTask *task= [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath,NSURLResponse *response) {                Nsstring * cache=[NSSearchPathForDirectorIEsInDomains(NSCachesDirectory,NSUserDomainMask,YES) lastObject];        cache =[cache stringByAppendingPathComponent:@"jereh.rar"];                NSURL * url=[NSURL fileURLWithPath:cache];                return url;                    } completionHandler:^(NSURLResponse *response,NSURL *filePath,NSError *error) {                if (error) {            NSLog(@"下载失败了");        }else{            NSLog(@"下载完成");        }            }];        //(3)开始任务(注意要写这一句)    [task resume];    }@end

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的AFNetworking框架的使用全部内容,希望文章能够帮你解决AFNetworking框架的使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1106467.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存