IOS study notes 5-asihttprequest

Source: Internet
Author: User

The usage of asihttprequest is not described much. There are a bunch of instances on the Internet.

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 // queue
10 queue = [[nsoperationqueue alloc] init];
11
12 btn1 = [uibutton buttonwithtype: uibuttontyperoundedrect];
13 [btn1 setframe: cgrectmake (50, 50,100, 50)];
14 [btn1 settitle: @ "Download" 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 // progress bar
22 progressview = [[uiprogressview alloc] initwithframe: cgrectmake (0.0f, 40366f, 220366f, 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 // multithreading asynchronous
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 // asynchronous
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 // This method is used when the returned content is read as text
84 nsstring * responsestring = [Request responsestring];
85
86 // This method is used when the returned content is read in binary format
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 // Synchronization
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.