較老版本的 AFNetworking 下載連接 http://pan.baidu.com/s/14Cxgahtml
將壓縮包中的文件夾拖入xcode工程項目中並引入以下的框架json
簡單的 JOSN 解析例子
static NSString *serverAddress = @"http://m.weather.com.cn/data/101110101.html";xcode
// 1.建立JSON操做對象
AFJSONRequestOperation *operation =
[AFJSONRequestOperation
JSONRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:serverAddress]]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"success -- %@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"failure -- %@", JSON);
}];服務器
// 2.執行對象的操做異步加載
[operation start];網絡
簡單的 XML 解析例子app
static NSString *serverAddress = @"http://flash.weather.com.cn/wmaps/xml/beijing.xml";
// 1.建立XML操做對象
AFXMLRequestOperation *operation =
[AFXMLRequestOperation
XMLParserRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:serverAddress]]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
NSLog(@"[$10000000$] success -- %@", XMLParser);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) {
NSLog(@"failure -- %@", XMLParser);
}];框架
// 2.執行對象的操做異步加載
[operation start];異步
HTTP POST請求例子async
-----------------------------------------------------------------------------------------------------函數
//內聯函數
NS_INLINE AFHTTPClient * createAFHTTPClient(NSString *baseURLString)
{
//建立一個AFHTTPClient的連接,僅需傳入服務器URL的String便可
return [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:baseURLString]];
}
NS_INLINE NSData * createJSONDataFromDict(NSDictionary *params)
{
//根據字典建立出JSON專用格式的NSData
return [NSJSONSerialization dataWithJSONObject:params
options:NSJSONWritingPrettyPrinted
error:nil];
}
-----------------------------------------------------------------------------------------------------
//服務器地址
static NSString *serverAddress = @"http://art.wooboo.com.cn/support/service.shtml";
//初始化一個本地的httpClient
AFHTTPClient *httpClient = createAFHTTPClient(serverAddress);
//完善httpClient並造成一個POST請求報文
NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST"
path:serverAddress
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSArray *paramsType = @[@{@"action": @"loadImg", @"artId": @"0"}];
//轉換字典數據爲JSON專用格式並再次轉換爲字符串
NSString *params = [[NSString alloc] initWithData:
createJSONDataFromDict(paramsType[0])
encoding:NSUTF8StringEncoding];
//進一步完善請求的內容 (Content-Disposition: form-data; name=#{name}")
[formData appendPartWithFormData:[params dataUsingEncoding:NSUTF8StringEncoding]
name:@"p"];
}];
//將請求報文發送到服務器進行連接
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation
setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", jsonObjectFromData(responseObject));
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error.");
}];
[operation start];
加載網絡圖片
-----------------------------------------------------------------------------------------------------
//內聯函數
NS_INLINE NSURL * netURL(NSString *netPath)
{
//網絡文件的URL
return [NSURL URLWithString:netPath];
}
NS_INLINE UIImage * imageFromBuddleByName(NSString *imageName)
{
//經過名字獲取buddle中圖片資源
return [UIImage imageNamed:imageName];
}
-----------------------------------------------------------------------------------------------------
- (void)setImageWithURL:(NSURL *)url
placeholderImage:(UIImage *)placeholderImage;
Creates and enqueues an image request operation, which asynchronously downloads the image from the specified URL. Any previous image request for the receiver will be cancelled. If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished.
-----------------------------------------------------------------------------------------------------
static NSString *picServerAddress =
@"http://wallpapers.wallbase.cc/high-resolution/wallpaper-2677423.jpg";
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[imageView setImageWithURL:netURL(picServerAddress)
placeholderImage:imageFromBuddleByName(@"1.png")];
-----------------------------------------------------------------------------------------------------