IOS Study Notes 26-http operations-asihttprequest (2)

Source: Internet
Author: User

Asihttprequest can also be used to submit request parameters to the server. In this example, the client sends an XML request and obtains the response from the server. The response is an XML string. Go to the Code directly. The Code has detailed comments.

First, make the following statement in the. h file:

#import <UIKit/UIKit.h>@interface ViewController : UIViewController@property (retain,nonatomic) UIActivityIndicatorView *indicator;@property (retain, nonatomic) IBOutlet UITextField *userNameTextField;@property (retain, nonatomic) IBOutlet UITextField *passwordTextField;- (IBAction)login:(id)sender;@end

Then the. m implementation file (only the main code section is listed here ):

// Use the asynchronous request and update the UI-(ibaction) login :( ID) sender {// The loading prompt box [self showtoast: @ "logging in... "];/* run the login request. The server uses XML for a servelet request running on Tomcat. The format is as follows: <document> <user id = "XXX" Password = "XXX"/> </document> the request result returns an XML string. I have omitted the server address here, because the server of the previous project is used, it cannot be disclosed here */_ block asihttprequest * request = [[asihttprequest alloc] initwithurl: [nsurl urlwithstring: @ "http: // xxx/Server/login "]; // construct a variable string request nsmutablestring * requestxml = [[nsmutablestring alloc] init]; [requestxml appendstring: @ "<document>"]; [requestxml appendstring: @ "<user id = \" "]; [requestxml appendstring: Self. usernametextfield. text]; [requestxml appendstring: @ "\" "]; [requestxml appendstring: @" Password = \ ""]; [requestxml appendstring: Self. passwordtextfield. text]; [requestxml appendstring: @ "\"/> "]; [requestxml appendstring: @" </document> "]; // converts the nsstring type to the nsdata type, the following parameter is the encoding type, here is the UTF-8 nsdata * requestdata = [requestxml datausingencoding: nsutf8stringencoding]; // method using the custom request parameter in asihttprequest [Request appendpostdata: requestdata]; // set the request method [Request setrequestmethod: @ "Post"]; // after the request is executed, the code in the block will be called [Request setcompletionblock: ^ {nslog (@ "success "); nslog (@ "% @", [Request responsestring]); [self. indicator stopanimating]; [alertview dismisswithclickedbuttonindex: 0 animated: Yes]; [self. indicator release]; [alertview release];}]; // if an exception occurs, the code in the block will be executed [Request setfailedblock: ^ {nslog (@ "failed"); [self. indicator stopanimating]; [alertview dismisswithclickedbuttonindex: 0 animated: Yes]; [self. indicator release]; [alertview release];}]; [Request startasynchronous]; [request release];} // create a custom pop-up prompt box-(void) showtoast :( nsstring *) message {alertview = [[uialertview alloc] initwithtitle: Message message: Nil delegate: Self cancelbuttontitle: Nil failed: nil, nil]; [alertview setbackgroundcolor: [uicolor clearcolor]; // The Show method must be called here; otherwise, indicator is not in uialerview [alertview show]; self. indicator = [[uiactivityindicatorviewalloc] initwithactivityindicatorstyle: uiactivityindicatorviewstylewhitelarge]; self. indicator. center = cgpointmake (alertview. bounds. size. width/2, alertview. bounds. size. height-40); // hide indicator self when stopping. indicator. hideswhenstopped = yes; // put uiactivityindicator as a sub-control in uialertview [alertview addsubview: Self. indicator]; [self. indicator startanimating];}

Let's take a look at the running effect and the returned information of the server:

After clicking log on, you can view the logon result returned by the server on the console:

The above is a small example of using asihttprequest to send data to the server and obtain the returned results. Using asihttprequest, there are many other powerful functions. You can refer to the official documentation for specific usage and usage.

If you are interested in Android and iOS, you can join our QQ discussion group. Here, we will only discuss the following:

IOS group: 220223507

Android group: 282552849

Welcome to my Sina Weibo chat: @ Tang Ren _ Ryan

Related Article

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: 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.