• ios 学习笔记 5 之 ASIHTTPRequest


      ASIHTTPRequest用法不多介绍,网上一堆,实例如图

    1 #import <UIKit/UIKit.h>
    2 #import "MainView.h"
    3
    4 @interface AppDelegate : UIResponder <UIApplicationDelegate>
    5
    6 @property (strong, nonatomic) UIWindow *window;
    7 @property (strong, retain) MainView *viewController;
    8
    9 @end
     1 - (void)dealloc
    2 {
    3 [_window release];
    4 [_viewController release];
    5 [super dealloc];
    6 }
    7
    8 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    9 {
    10 CGRect screenBounds=[[UIScreen mainScreen]applicationFrame];
    11 CGRect windowBounds=screenBounds;
    12 windowBounds.origin.y=0;
    13 self.window = [[[UIWindow alloc] initWithFrame:screenBounds] autorelease];
    14 // Override point for customization after application launch.
    15 self.viewController=[[MainView alloc]initWithFrame:windowBounds];
    16 self.window.backgroundColor = [UIColor whiteColor];
    17 [self.window addSubview: self.viewController];
    18 [self.window makeKeyAndVisible];
    19 return YES;
    20 }
     1 #import <UIKit/UIKit.h>
    2 #import "ASIHTTPRequest.h"
    3
    4 @interface MainView : UIView
    5 {
    6 UIButton *btn1;
    7 NSOperationQueue *queue;
    8 ASIHTTPRequest *request;
    9 UIProgressView *progressView;
    10 }
    11
    12 - (IBAction)grabURL:(id)sender;
    13
    14 @end
      1 #import "MainVIew.h"
    2
    3 @implementation MainView
    4
    5 - (id)initWithFrame:(CGRect)frame
    6 {
    7 self = [super initWithFrame:frame];
    8 if (self) {
    9 //队列
    10 queue = [[NSOperationQueue alloc] init];
    11
    12 btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    13 [btn1 setFrame:CGRectMake(50, 50, 100, 50)];
    14 [btn1 setTitle:@"下载" forState:UIControlStateNormal];
    15
    16 btn1.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    17 btn1.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 16);
    18 [btn1 addTarget:self
    19 action:@selector(grabURLInTheBackground:)
    20 forControlEvents:UIControlEventTouchUpInside];
    21 //进度条
    22 progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0.0f, 40.0f, 220.0f, 90.0f)];
    23 [progressView setProgressViewStyle: UIProgressViewStyleDefault];
    24
    25 [self addSubview:btn1];
    26 [self addSubview:progressView];
    27 }
    28 return self;
    29 }
    30
    31 -(void) dealloc
    32 {
    33 [request clearDelegatesAndCancel];
    34 [request release];
    35 request = nil;
    36 [progressView release];
    37 [queue release];
    38 [btn1 release];
    39 [super dealloc];
    40 }
    41
    42 //多线程异步
    43 - (IBAction)grabURLInTheBackground:(id)sender
    44 {
    45
    46 NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
    47 request = [ASIHTTPRequest requestWithURL:url];
    48 [request setDelegate:self];
    49 [request setDidFinishSelector:@selector(requestDone:)];
    50 [request setDidFailSelector:@selector(requestWentWrong:)];
    51 [request setDownloadProgressDelegate:progressView];
    52 [queue addOperation:request];
    53
    54 }
    55
    56 - (void)requestDone:(ASIHTTPRequest *)request
    57 {
    58 NSString *responseString = [request responseString];
    59 NSLog(@"responseString=%@",responseString);
    60 }
    61
    62 - (void)requestWentWrong:(ASIHTTPRequest *)request
    63 {
    64 NSError *error = [request error];
    65 }
    66
    67 //异步
    68 - (IBAction)grabURLInBackground:(id)sender
    69 {
    70
    71 NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
    72
    73 request= [ASIHTTPRequest requestWithURL:url];
    74
    75 [request setDelegate:self];
    76
    77 [request startAsynchronous];
    78
    79 }
    80
    81 - (void)requestFinished:(ASIHTTPRequest *)request
    82 {
    83 // 当以文本形式读取返回内容时用这个方法
    84 NSString *responseString = [request responseString];
    85
    86 // 当以二进制形式读取返回内容时用这个方法
    87 NSData *responseData = [request responseData];
    88
    89 NSLog(@"responseString=%@",responseString);
    90
    91 [responseString autorelease];
    92 [responseData autorelease];
    93
    94
    95 }
    96
    97 - (void)requestFailed:(ASIHTTPRequest *)request
    98 {
    99
    100 NSError *error = [request error];
    101 [error autorelease];
    102
    103 }
    104
    105 //同步
    106 - (IBAction)grabURL:(id)sender
    107 {
    108 NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
    109
    110 request = [ASIHTTPRequest requestWithURL:url];
    111
    112 [request startSynchronous];
    113
    114 NSError *error = [request error];
    115
    116 if (!error) {
    117
    118 NSString *response = [request responseString];
    119 NSLog(@"response=%@",response);
    120
    121 }
    122
    123 }
    124
    125 @end






  • 相关阅读:
    httpclient + TestNG 接口自动测试 第二章
    httpclient + TestNG 接口自动测试 第一章
    Kafka-manager启动命令
    多台服务器搭建Spark集群
    Scala学习 -- 基础语法
    Spark学习 -- RDD
    Spark 学习
    Angular constructor和OnInit的区别和适用场景
    TypeScript基础学习 —— 变量声明
    TypeScript基础学习 —— 基础类型
  • 原文地址:https://www.cnblogs.com/hubj/p/2327990.html
Copyright © 2020-2023  润新知