ASI框架使用全集講解

jopen 9年前發布 | 931 次閱讀 Objective-C IOS

#import "ViewController.h"

import "ASIHTTPRequest.h"

import "ASIFormDataRequest.h"

import "DACircularProgressView.h"

@interface ViewController ()<ASIHTTPRequestDelegate>

@property(nonatomic,strong) NSMutableData *data;

@property(nonatomic,weak) DACircularProgressView * da; @end

@implementation ViewController

  • (NSMutableData *)data{
if (_data==nil) {
    _data=[NSMutableData data];
}
return _data;

}

  • (void)viewDidLoad { [super viewDidLoad];

    self.view.backgroundColor=[UIColor greenColor];

    DACircularProgressView * da=[[DACircularProgressView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

    self.da=da; self.da.center=self.view.center; [self.view addSubview:da];

}

-(void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event{

//1 ASI 同步get請求

// [self _synGet];

//2 ASI 異步get請求

// [self _asynGet];

//3 ASI 異步get請求(block)

// [self _asynGetBlock];

//4 ASI 同步Post請求

// [self _synPost];

//5 ASI 異步Post請求

// [self _asynPost];

// 6ASI 下載

// [self _downLoad];

// 7ASI 上傳
[self _upLoad];


}

//同步get請求

  • (void)_synGet{
NSURL * url=[NSURL URLWithString:@"http://localhost/logo.php?userName=jereh&pwd=123"];

//1 封裝請求
ASIHTTPRequest * request=[[ASIHTTPRequest alloc] initWithURL:url];
//2 發送請求
[request startSynchronous];
//3 獲取響應數據
NSData * data=request.responseData;
NSString * result=[[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding];
NSLog(@"%@",result);

}

//異步get請求

  • (void)_asynGet{
NSURL * url=[NSURL URLWithString:@"http://localhost/logo.php?userName=jereh&pwd=123"];

//1 封裝請求
ASIHTTPRequest * request=[[ASIHTTPRequest alloc] initWithURL:url];

request.delegate=self;

//2 發送請求
[request startAsynchronous];



}

//異步get請求block

  • (void)_asynGetBlock{
NSURL * url=[NSURL URLWithString:@"http://localhost/logo.php?userName=jereh&pwd=123"];

//1 封裝請求
ASIHTTPRequest * request=[[ASIHTTPRequest alloc] initWithURL:url];

//2 發送請求
[request startAsynchronous];


//3 重寫block
 [request setDataReceivedBlock:^(NSData *data) {
     [self.data appendData:data];




 }];

 [request setHeadersReceivedBlock:^(NSDictionary *responseHeaders) {

 }];

[request setFailedBlock:^{

}];


[request setCompletionBlock:^{
    NSString * str=  [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",str);
}];

}

//同步Post請求block

  • (void) _synPost{
NSURL * url=[NSURL URLWithString:@"http://localhost/loginPost.php"];

ASIFormDataRequest * form=[[ASIFormDataRequest alloc] initWithURL:url];

//設置請求參數
[form setPostValue:@"jereh" forKey:@"userName"];
[form setPostValue:@"123" forKey:@"pwd"];

[form startSynchronous];

NSString * str= form.responseString;
NSLog(@"%@",str);

}

//同步Post請求block

  • (void) _asynPost{

    NSURL * url=[NSURL URLWithString:@"http://localhost/loginPost.php&quot;];

    ASIFormDataRequest * form=[[ASIFormDataRequest alloc] initWithURL:url];

    //設置請求參數 [form setPostValue:@"jereh" forKey:@"userName"]; [form setPostValue:@"123" forKey:@"pwd"];

    form.delegate=self;

    [form startSynchronous];

}

  • (void) _downLoad{

    NSURL * url=[NSURL URLWithString:@"http://localhost/test.rar&quot;];

    //1 封裝請求 ASIHTTPRequest * request=[[ASIHTTPRequest alloc] initWithURL:url];

    //2 dest path NSString *path=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; path =[path stringByAppendingPathComponent:@"new.rar"]; NSLog(@"%@",path);

    request.downloadDestinationPath=path;

    request.downloadProgressDelegate=self.da;

    //3 請求 [request startAsynchronous];

}

//上傳

  • (void) _upLoad{

    NSURL * url=[NSURL URLWithString:@"http://localhost/upload.php&quot;];

    ASIFormDataRequest * form=[[ASIFormDataRequest alloc] initWithURL:url];

NSString * path=[[NSBundle mainBundle] pathForResource:@"default.png" ofType:nil];

//設置文件參數
[form setFile:path withFileName:@"new.png" andContentType:@"image/png" forKey:@"file"];


form.uploadProgressDelegate=self.da;

[form startAsynchronous];

}

pragma mark - ASIHTTPRequest代理

  • (void)request:(ASIHTTPRequest )request didReceiveData:(NSData )data{

    [self.data appendData:data]; }

  • (void)request:(ASIHTTPRequest )request didReceiveResponseHeaders:(NSDictionary )responseHeaders{

}

  • (void)requestFinished:(ASIHTTPRequest *)request{

    NSString * str= [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding]; NSLog(@"%@",str);

}

  • (void)requestFailed:(ASIHTTPRequest *)request{

}

@end </pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!