ASIHTTPRequest一个实例
来源:互联网 发布:office画图软件 visio 编辑:程序博客网 时间:2024/06/18 18:26
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)sender107 {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
- ASIHTTPRequest一个实例
- ASIHTTPRequest
- ASIHTTPRequest
- ASIHTTPRequest
- ASIHTTPRequest
- ASIHTTPRequest
- ASIHTTPRequest
- ASIHTTPRequest
- asihttprequest
- ASIHTTPRequest
- ASIHTTPRequest
- ASIHTTPRequest
- ASIHttpRequest
- ASIHTTPRequest
- ASIHttpRequest
- ASIHTTPRequest
- ASIHTTPRequest
- ASIHTTPRequest
- asihttprequest简单异步
- 说说VS2010中的插件
- Android 开发环境的搭建及离线安装(基础下载)
- js中使用弹出窗体
- Array、Linked和Hash的区别
- ASIHTTPRequest一个实例
- 在Linux下安装JDK及环境设置
- 指针
- weblogic下抛java.lang.IllegalStateException: response already committed tomcat下正常
- 四大Linux备份工具比较与操作实例
- 让数据库变快的10个建议
- WEPopover的使用
- ubuntu清理系统垃圾与备份
- STL之全排列(百度迅雷笔试题)