本文转载自:http://blog.youkuaiyun.com/kmyhy/article/details/6524893
项目下载地址: http://github.com/pokeb/asi-http-request/tarball/master
下载后将文件解压缩到任意目录。
打开该目录,其目录中包含了:
一个iPhone Xcode项目(源文件)
一个Mac Xcode项目(源文件)
一个iPhone下使用的Sample Code(源文件)
一个Mac下使用的Sample Code(源文件)
一个Readme.texttile,关于该项目的介绍
其实所有的内容都在其中了,如果你是初学者,不知到怎么下手,可以看 http://allseeing-i.com/ASIHTTPRequest/How-to-use
这里有一份详细的入门指南。
现在,我们要做的就是,在自己的项目中使用它。
一、在项目中使用ASIHTTPRequest
1、拷贝源文件到项目中
ASIHTTPRequest 是一个开源项目,要使用他,直接拷贝项目源文件到你的项目中,包括下列文件(即Classes下所有文件和External/Reachability下所有文件):
- ASIHTTPRequestConfig.h
- ASIHTTPRequestDelegate.h
- ASIProgressDelegate.h
- ASICacheDelegate.h
- ASIHTTPRequest.h
- ASIHTTPRequest.m
- ASIDataCompressor.h
- ASIDataCompressor.m
- ASIDataDecompressor.h
- ASIDataDecompressor.m
- ASIFormDataRequest.h
- ASIInputStream.h
- ASIInputStream.m
- ASIFormDataRequest.m
- ASINetworkQueue.h
- ASINetworkQueue.m
- ASIDownloadCache.h
- ASIDownloadCache.m
对于 iPhone,还要拷贝下列文件:
- ASIAuthenticationDialog.h
- ASIAuthenticationDialog.m
- Reachability.h (External/Reachability 目录 )
- Reachability.m (External/Reachability 目录 )
2、添加依赖库
ASIHTTPRequest 依赖于以下5个框架或库:
CFNetwork, SystemConfiguration, MobileCoreServices, CoreGraphics 和 libz1.2.3。
依次将上述库和框架添加到 target 的 Linked Libraries 中。
二、简单的同步请求示例
新建 iOS 项目,加入必需的源文件和 Linked Libraries。
往 MainWindow.xib 中添加一个 UIView和一个 UIButton,在 delegate 中添加相应的出口并在IB中进行连接。
编写按钮的Touch up inside代码,并连接到UIButton:
-( IBAction )goURL{
NSURL *url = [ NSURL URLWithString : @"http://localhost/interface/GetDept" ];
// 构造 ASIHTTPRequest 对象
ASIHTTPRequest *request = [ ASIHTTPRequest requestWithURL :url];
// 开始同步请求
[request startSynchronous ];
NSError *error = [request error ];
assert (!error);
// 如果请求成功,返回 Response
NSString *response = [request responseString ];
NSLog ( @"%@" ,response);
}
别忘了在适当的地方导入ASIHTTPRequest: #import "ASIHTTPRequest.h"
分别保存IB和Xcode中所做的更改, ⌘+B 编译。
三、简单的异步请求示例
将上述代码修改为:
-( IBAction )goURL{
NSURL *url = [ NSURL URLWithString : @"http://localhost/interface/GetDept" ];
ASIHTTPRequest *request = [ ASIHTTPRequest requestWithURL :url];
// 设定委托,委托自己实现异步请求方法
[request setDelegate : self ];
// 开始异步请求
[request startAsynchronous ];
}
并实现一系列委托方法:
// 请求结束,获取 Response 数据
- ( void )requestFinished:( ASIHTTPRequest *)request
{
NSString *responseString = [request responseString ]; // 对于 2 进制数据,使用: NSData *responseData = [request responseData];
NSLog ( @"%@" ,responseString);
button . enabled = YES ;
}
// 请求失败,获取 error
- ( void )requestFailed:( ASIHTTPRequest *)request
{
NSError *error = [request error ];
NSLog ( @"%@" ,error. userInfo );
button . enabled = YES ;
}
从OS X 10.6及iOS 4.0起,支持块语法,你也可以使用块语法调用ASIHTTPRequest:
-( IBAction )goURL{
NSURL *url = [ NSURL URLWithString : @"http://localhost/interface/GetDept" ];
__block ASIHTTPRequest *request = [ ASIHTTPRequest requestWithURL :url];
// ASIHTTPRequest 支持 iOS 4.0 的块语法,你可以把委托方法定义到块中
[request setCompletionBlock :^{
// 请求响应结束,返回 responseString
NSString *responseString = [request responseString ]; // 对于 2 进制数据,使用 NSData 返回 NSData *responseData = [request responseData];
NSLog ( @"%@" ,responseString);
}];
[request setFailedBlock :^{
// 请求响应失败,返回错误信息
NSError *error = [request error ];
NSLog ( @"error:%@" ,[error userInfo ]);
}];
[request startAsynchronous ];
}