中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

AFNetworking框架的使用

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
#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];
    
   
//    默認(rèn)就是異步的請(qǐng)求!
}

/**
 *  get 請(qǐng)求
 */
- (void) sendGet{

    AFHTTPRequestOperationManager * mamaner=[AFHTTPRequestOperationManager manager];//單例
    
    //設(shè)置解析返回的數(shù)據(jù)的類型(默認(rèn)就是解析json的)(可以設(shè)置,有三種)
//    mamaner.responseSerializer=[AFHTTPResponseSerializer serializer];//不管返回什么樣的數(shù)據(jù),統(tǒng)一解析成二進(jìn)制數(shù)據(jù)
//    mamaner.responseSerializer = [AFXMLParserResponseSerializer serializer];//返回的是xml的,使用這個(gè)
//    mamaner.responseSerializer = [AFJSONResponseSerializer serializer];//默認(rèn)的
    
    //get請(qǐng)求兩種寫法
    //(1)寫法一
    NSString * url=@"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 * url=@"http://192.168.2.162/logo.php";
//    NSDictionary * dic=@{@"userName":@"jereh",@"pwd":@"123"};
//    [mamaner GET:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        
//        NSLog(@"%@",responseObject);
//        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"%@",error);
//    }];
    
}

/**
 *  post 請(qǐng)求
 */
- (void) sendPost{
    
    AFHTTPRequestOperationManager * mamaner=[AFHTTPRequestOperationManager manager];
    
    //    mamaner.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    
    
    NSString * url=@"http://192.168.2.162/loginPost";
    NSDictionary * dic=@{@"userName":@"jereh",@"pwd":@"123"};
    
    [mamaner POST:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"%@",responseObject);

        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
 
    
}




/**
 *  post 請(qǐng)求(上傳,使用post)
 */
- (void) upLoad{

    AFHTTPRequestOperationManager * mamaner=[AFHTTPRequestOperationManager manager];

    mamaner.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSString * url=@"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, NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
}




/**
 *  post 請(qǐng)求(下載,get請(qǐng)求)
 */
- (void) downLoad{
    
    //(0)創(chuàng)建manager對(duì)象
    NSURLSessionConfiguration * config=[NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager * manager=[[AFURLSessionManager alloc] initWithSessionConfiguration:config];
    
    
    //(1)監(jiān)控下載進(jìn)度
    [manager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
        //注意當(dāng)前線程是子線程,需要返回主線程刷新數(shù)據(jù)
        CGFloat progress=totalBytesWritten*1.0/totalBytesExpectedToWrite;//寫入的比上總共的
        dispatch_sync(dispatch_get_main_queue(), ^{
            self.progress.progress=progress;
        });
    }];
    
    //(2)請(qǐng)求
    NSURLRequest * request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.2.162/test.rar"]];
    //注意下邊的方法有返回值,block也有一個(gè)返回值
    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)開始任務(wù)(注意要寫這一句)
    [task resume];
    
}


@end

標(biāo)簽: isp seo

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇: JAVA判斷中文字符

下一篇: python-pcap模塊解析mac地址