ASIHTTPRequest是一個(gè)對(duì)CFNetwork API進(jìn)行了封裝,并且使用起來非常簡(jiǎn)單的一套API,用Objective-C編寫,可以很好的應(yīng)用在Mac OS X系統(tǒng)和iOS平臺(tái)的應(yīng)用程序中。ASIHTTPRequest適用于基本的HTTP請(qǐng)求,和基于REST的服務(wù)之間的交互。如果想要在項(xiàng)目中使用asihttprequest,需要進(jìn)行如下的配置:
1:首先需要有ASIHTTPRequest 的資源包。下載地址:http://down.51cto.com/data/1984747
2:在xcode 中右鍵項(xiàng)目選擇“Add Files to ”選項(xiàng):如下圖
3:選擇第1步準(zhǔn)備好的asi的類庫(kù)源文件,如下圖:
選擇Copy items if needed .
這樣ASI的類庫(kù)源文件就已經(jīng)添加完畢了。接下來,我們要設(shè)置ASI的源文件不使用ARC機(jī)制,具體做法就是在項(xiàng)目中的ASI源文件中的Complier Flags 中設(shè)置為 -fno-objc-arc 。首先選中項(xiàng)目的targets 的Build Phases ,如下圖:
就是雙擊每個(gè)ASI源文件的Complier Flags 然后填上 -fno-objc-arc 。
接下來需要在frameworks 中添加libz.dylib ,在項(xiàng)目的targets 選擇 link Binary With libraried ,點(diǎn)擊+號(hào)進(jìn)行添加,如下圖:
用ASI框架進(jìn)行通信的簡(jiǎn)單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{
//測(cè)試用
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];
//生產(chǎn)的用
// 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];
}
//登錄驗(yàn)證請(qǐng)求正常
-(void)loginRequestDone:(ASIHTTPRequest*)request{
NSData* responseData=[request responseData];
NSString* responseStr=[request responseString];
// 返回的數(shù)據(jù)
NSLog(@"%@",request.responseString);
}
//登錄驗(yàn)證請(qǐng)求失敗
-(void)loginRequestFail:(ASIHTTPRequest*)request{
//取消等待框
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"服務(wù)器錯(cuò)誤" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil,nil];
[alertView show];
NSLog(@"服務(wù)器錯(cuò)誤%@",request.error);
}
@end
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。