网络之AFNetworking

  AFNetsorking作为功能全面的网络第三方,既通俗好用又与时俱进-及时的更新使用了NSURLSession,不得不爱。

AFNetsorking使用:

1,AFNetsorking GET请求 

  ①:URL

  ②:字典存放所需的参数

#pragma mark - get请求
- (IBAction)getAction:(UIButton *)sender {

// NSURL * url =[NSURL URLWithString:@"http://loaclhost/first.html"];

AFHTTPSessionManager * manager =[AFHTTPSessionManagermanager];

NSString * urlStr =@"http://localhost/logo.php";

//  AFHTTPResponseSerializer * serializer
//设置响应数据的格式为二进制(NSData)
// manager.responseSerializer = [AFHTTPResponseSerializer serializer];
/** 返回的类型是后台给出的类型,设置可接受的类型 */
    manager.responseSerializer.acceptableContentTypes=[NSSetsetWithObject:@"text/html"];

//设置参数
NSDictionary * dict = @{@"userName":@"jredu",@"psw":@"123"};

//网络请求方法
    [manager GET:urlStr parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id_Nullable responseObject) {

NSString * str = [[NSStringalloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

NSLog(@"成功:===%@===%@",responseObject,str);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

NSLog(@"失败===%@",error);

    }];


}

 2,AFNetsorking POST请求

#pragma mark - POST请求
- (IBAction)postAction:(UIButton *)sender {

//请求的URL
NSString * urlStr = @"http://localhost/login_post.php";

//>初始化单例manager
AFHTTPSessionManager * manager = [AFHTTPSessionManagermanager];

//>设置接收类型
    manager.responseSerializer.acceptableContentTypes=[NSSetsetWithObject:@"text/html"];

//设置参数
NSDictionary * param = @{@"userName":@"jredu",@"psw":@"123"};

//>发起网络请求
    [manager POST:urlStr parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id_Nullable responseObject) {

NSLog(@"成功请求== %@",responseObject);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

NSLog(@"请求失败==%@",error);
    }];

}

3,AFNetsorking 下载

 

 4,AFNetsorking  上传

#pragma mark - 上传数据
- (IBAction)upLoadAction:(UIButton *)sender {

//上传PNG图片
/*
//    NSString * path=@"http://192.168.0.215/upload.php";
//    
//    AFHTTPSessionManager * manager=[AFHTTPSessionManager manager];
//    
//    [manager POST:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//        
//        //1 将图片加载出来并转成二进制
//        UIImage * image=[UIImage imageNamed:@"icon.png"];
//        
//        NSData * data=UIImagePNGRepresentation(image);
//        
//        //        NSData * data2=UIImageJPEGRepresentation(image, 0.5);
//        
//        
//        [formData appendPartWithFileData:data name:@"file" fileName:@"zhangsan.png" mimeType:@"image/png"];
//        
//    } progress:^(NSProgress * _Nonnull uploadProgress) {
//        
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"成功");
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"失败");
//    }];
    */

NSString * urlStr = @"http://localhost/upload.php";

AFHTTPSessionManager * manager = [AFHTTPSessionManagermanager];
/** 设置数据为二进制(NSData) */
     manager.responseSerializer = [AFHTTPResponseSerializerserializer];

//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

    [manager POST:urlStr parameters:nilconstructingBodyWithBlock:^(id<AFMultipartFormData>_Nonnull formData) {

UIImage * image = [UIImageimageNamed: @"icon.png"];

//        NSData * data = UIImagePNGRepresentation(image);
NSData * data = UIImageJPEGRepresentation(image, 0.55);

//        formData =data;
        [formData appendPartWithFileData:data name:@"file"fileName:@"spiderMan.jpg"mimeType:@"image/jpeg"];

    } progress:^(NSProgress * _Nonnull uploadProgress) {


    } success:^(NSURLSessionDataTask * _Nonnull task, id_Nullable responseObject) {
/** 请求网络成功 with下载的数据和请求的数据*/
NSLog(@"seccuss");



    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
/** 请求网络失败,返回已经下载的数据和错误信息 */
NSLog(@"filed==%@",error);

    }];
}

 

   的确,AFNetsorking 功能全面,使用也不复杂。但是想要其真正的为我所用,进一步的封装是必不可少的。

 

posted @ 2016-08-03 21:31  wangwei_Carry  阅读(470)  评论(0编辑  收藏  举报