关闭

ASIHTTPRequest一个实例

1367人阅读 评论(0) 收藏 举报

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
from:http://www.cnblogs.com/pengyingh/articles/2354270.html


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1703980次
    • 积分:15604
    • 等级:
    • 排名:第686名
    • 原创:209篇
    • 转载:274篇
    • 译文:0篇
    • 评论:257条