ios如何使用ASIHttpRequest進(jìn)行http通信-創(chuàng)新互聯(lián)

ASIHTTPRequest是一個對CFNetwork API進(jìn)行了封裝,并且使用起來非常簡單的一套API,用Objective-C編寫,可以很好的應(yīng)用在Mac OS X系統(tǒng)和iOS平臺的應(yīng)用程序中。ASIHTTPRequest適用于基本的HTTP請求,和基于REST的服務(wù)之間的交互。如果想要在項(xiàng)目中使用asihttprequest,需要進(jìn)行如下的配置:

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),峨眉山企業(yè)網(wǎng)站建設(shè),峨眉山品牌網(wǎng)站建設(shè),網(wǎng)站定制,峨眉山網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,峨眉山網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

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

2:在xcode 中右鍵項(xiàng)目選擇“Add Files to ”選項(xiàng):如下圖

ios 如何使用 ASIHttpRequest進(jìn)行http通信

3:選擇第1步準(zhǔn)備好的asi的類庫源文件,如下圖:

ios 如何使用 ASIHttpRequest進(jìn)行http通信

選擇Copy items if needed .

這樣ASI的類庫源文件就已經(jīng)添加完畢了。接下來,我們要設(shè)置ASI的源文件不使用ARC機(jī)制,具體做法就是在項(xiàng)目中的ASI源文件中的Complier Flags 中設(shè)置為 -fno-objc-arc 。首先選中項(xiàng)目的targets 的Build Phases ,如下圖:

ios 如何使用 ASIHttpRequest進(jìn)行http通信

就是雙擊每個ASI源文件的Complier Flags 然后填上  -fno-objc-arc 。

接下來需要在frameworks 中添加libz.dylib ,在項(xiàng)目的targets 選擇 link Binary With libraried ,點(diǎn)擊+號進(jìn)行添加,如下圖:

ios 如何使用 ASIHttpRequest進(jìn)行http通信

用ASI框架進(jìn)行通信的簡單DEMO如下:

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

@interface ViewController ()<ASIHTTPRequestDelegate>
{

}

@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];

   //生產(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)證請求正常
-(void)loginRequestDone:(ASIHTTPRequest*)request{
   NSData* responseData=[request responseData];
   NSString* responseStr=[request responseString];
   // 返回的數(shù)據(jù)
   NSLog(@"%@",request.responseString);
}

//登錄驗(yàn)證請求失敗
-(void)loginRequestFail:(ASIHTTPRequest*)request{
   //取消等待框
   UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"服務(wù)器錯誤" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil,nil];
   [alertView show];
   NSLog(@"服務(wù)器錯誤%@",request.error);
}

@end

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

當(dāng)前標(biāo)題:ios如何使用ASIHttpRequest進(jìn)行http通信-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://muchs.cn/article16/dcgpgg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司網(wǎng)站導(dǎo)航、外貿(mào)建站網(wǎng)站改版、品牌網(wǎng)站設(shè)計、標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)