新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

ios如何使用ASIHttpRequest进行http通信-创新互联

ASIHTTPRequest是一个对CFNetwork API进行了封装,并且使用起来非常简单的一套API,用Objective-C编写,可以很好的应用在Mac OS X系统和iOS平台的应用程序中。ASIHTTPRequest适用于基本的HTTP请求,和基于REST的服务之间的交互。如果想要在项目中使用asihttprequest,需要进行如下的配置:

成都创新互联是一家集网站建设,峨眉山企业网站建设,峨眉山品牌网站建设,网站定制,峨眉山网站建设报价,网络营销,网络优化,峨眉山网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

1:首先需要有ASIHTTPRequest 的资源包。下载地址:http://down.51cto.com/data/1984747

2:在xcode 中右键项目选择“Add Files to ”选项:如下图

ios 如何使用 ASIHttpRequest进行http通信

3:选择第1步准备好的asi的类库源文件,如下图:

ios 如何使用 ASIHttpRequest进行http通信

选择Copy items if needed .

这样ASI的类库源文件就已经添加完毕了。接下来,我们要设置ASI的源文件不使用ARC机制,具体做法就是在项目中的ASI源文件中的Complier Flags 中设置为 -fno-objc-arc 。首先选中项目的targets 的Build Phases ,如下图:

ios 如何使用 ASIHttpRequest进行http通信

就是双击每个ASI源文件的Complier Flags 然后填上  -fno-objc-arc 。

接下来需要在frameworks 中添加libz.dylib ,在项目的targets 选择 link Binary With libraried ,点击+号进行添加,如下图:

ios 如何使用 ASIHttpRequest进行http通信

用ASI框架进行通信的简单DEMO如下:

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

@interface ViewController ()
{

}

@end

@implementation ViewController

- (void)viewDidLoad {
   [super viewDidLoad];
   // Do any 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 that can be recreated.
}

//登录
-(void)login{

   //测试用
   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];

   //生产的用
   //   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];

}

//登录验证请求正常
-(void)loginRequestDone:(ASIHTTPRequest*)request{
   NSData* responseData=[request responseData];
   NSString* responseStr=[request responseString];
   // 返回的数据
   NSLog(@"%@",request.responseString);
}

//登录验证请求失败
-(void)loginRequestFail:(ASIHTTPRequest*)request{
   //取消等待框
   UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"服务器错误" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil,nil];
   [alertView show];
   NSLog(@"服务器错误%@",request.error);
}

@end

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站标题:ios如何使用ASIHttpRequest进行http通信-创新互联
文章源于:http://www.wjwzjz.com/article/dcgpgg.html
在线咨询
服务热线
服务热线:028-86922220
TOP