How iOS uses ASIHTTPRequest for HTTP communication

Source: Internet
Author: User

ASIHTTPRequest is a set of APIs that encapsulates the Cfnetwork API and is very simple to use, written in objective-c, and can be applied to Mac OS x systems and iOS applications. ASIHTTPRequest applies to basic HTTP requests, and to the interaction between rest-based services. If you want to use ASIHTTPRequest in your project, you need to configure the following:

1: First, a resource bundle with ASIHTTPRequest is required. : http://down.51cto.com/data/1984747

2: Right-click Project in Xcode Select "Add Files to" option:

650) this.width=650; "title=" screenshot 2015-02-27 a.m. 11.24.07.png "alt=" wkiom1tv5qxymwqaaaqfhbwrkpw217.jpg "src="/HTTP/ S3.51cto.com/wyfs02/m01/59/ee/wkiom1tv5qxymwqaaaqfhbwrkpw217.jpg "/>

3: Select the 1th step to prepare the ASI library source files such as:

650) this.width=650; "title=" screenshot 2015-02-27 a.m. 11.26.36.png "alt=" wkiom1tv5wfayd6qaazi4jfdpbc056.jpg "src="/HTTP/ S3.51cto.com/wyfs02/m02/59/ee/wkiom1tv5wfayd6qaazi4jfdpbc056.jpg "/>

Select Copy items if needed.

In this way, the ASI library source files have been added. Next, we want to set up the ASI source file without using the ARC mechanism, specifically set to-fno-objc-arc in the Complier Flags in the ASI source file in the project. First select the targets build phases for the project, such as:

650) this.width=650; "title=" screenshot 2015-02-27 a.m. 11.38.49.png "alt=" wkiom1tv52sse9ataavfsfhpag0139.jpg "src="/HTTP/ S3.51cto.com/wyfs02/m01/59/ee/wkiom1tv52sse9ataavfsfhpag0139.jpg "/>

is to double-click the Complier Flags for each ASI source file and fill in the -fno-objc-arc .

Next you need to add libz.dylib in frameworks, select Link Binary with libraried in the project targets, click on the + sign to add, such as:

650) this.width=650; "title=" screenshot 2015-02-27 pm 2.12.55.png "alt=" wkiom1twc6ltvogzaan_qpvi0ty814.jpg "src="/HTTP/ S3.51cto.com/wyfs02/m00/59/ef/wkiom1twc6ltvogzaan_qpvi0ty814.jpg "/>

The simple demo of communication using the ASI framework is as follows:

#import "ViewController.h"
#import "ASIFormDataRequest.h"
#import "JSONKit.h"

@interface Viewcontroller () <ASIHTTPRequestDelegate>
{

}

@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];
Additional setup after loading the view, typically from a nib.

[Self.bt addtarget:self Action: @selector (login) Forcontrolevents:uicontroleventtouchdown];

}

-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}


Login
-(void) login{

For testing
Nsmutabledictionary *diclogin=[nsmutabledictionary Dictionary];
[Diclogin setobject:@ "1" forkey:@ "userid"];
NSString *strjson = [Diclogin jsonstring];
NSLog (@ "%@", Strjson);
nsurl* url = [Nsurl urlwithstring:@ "http://10.129.240.243:7001/initblogserver/handlogin?userid=1"];
asiformdatarequest* Request=[asiformdatarequest Requestwithurl:url];
request.delegate=self;
[Request Setdidfinishselector: @selector (loginrequestdone:)];
[Request Setdidfailselector: @selector (loginrequestfail:)];
[Request startasynchronous];

The use of production
Nsmutabledictionary *diclogin=[nsmutabledictionary Dictionary];
nsstring* Smscode=self.ctxt.text;
[Diclogin setobject:smscode forkey:@ "Smscode"];
NSString *strjson = [Diclogin jsonstring];
asiformdatarequest* Request=[asiformdatarequest Requestwithurl:login_serv];
[Request Setpostvalue:strjson forkey:@ "Logjson"];
request.delegate=self;
[Request Setdidfinishselector: @selector (loginrequestdone:)];
[Request Setdidfailselector: @selector (loginrequestfail:)];
[Request startasynchronous];

}

Logon Verification Request OK
-(void) Loginrequestdone: (asihttprequest*) request{
nsdata* Responsedata=[request ResponseData];
nsstring* Responsestr=[request responsestring];
The returned data
NSLog (@ "%@", request.responsestring);
}


Logon Verification request failed
-(void) Loginrequestfail: (asihttprequest*) request{
Cancel the Wait box
Uialertview *alertview = [[Uialertview alloc]initwithtitle:@ "Server error" Message:nil delegate:self CancelButtonTitle:nil otherbuttontitles:@ "OK", Nil,nil];
[Alertview show];
NSLog (@ "Server error%@", request.error);
}

@end

This article from "Mo Panic, raise straight forward" blog, please be sure to keep this source http://kunyali.blog.51cto.com/4890065/1615663

How iOS uses ASIHTTPRequest for HTTP communication

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.