AFNetworking 使用總結 (用法+JSON解析)

來源:互聯網
上載者:User

標籤:

« AFNetworking 圖片的本機快取問題Get application bundle seed ID in iOS »AFNetworking 使用總結 (用法+JSON解析)  

From:http://www.coneboy.com/?p=716

這幾天在評論頁面的時候發現get請求後加入2個以上的漢字會出現BadURL提示,斷點調試發現是多中文字元惹的禍,所以在URL使用以前先轉碼成UTF-8即可
這裡順便說明下自己使用AFNetworking 的常用方法。

目前我只是用到
#import “AFHTTPClient.h”;
#import “AFHTTPRequestOperation.h”
這兩個類

發起一個請求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 NSString *URLTmp = @”http://www.coneboy.com”;
NSString *URLTmp1 = [URLTmp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  //轉碼成UTF-8  否則可能會出現錯誤
URLTmp = URLTmp1;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: URLTmp]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@”Success: %@”, operation.responseString);

 

NSString *requestTmp = [NSString stringWithString:operation.responseString];
NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
//系統內建JSON解析
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@”Failure: %@”, error);
[SVProgressHUD dismissWithError:@”提交失敗,請重試”];
}];
[operation start];
}

AFNetworking 下載圖片
AFNETWorking 下載圖片是非同步並且可緩衝到cache裡面 非常好用不會阻塞主線程。用過的都說好!而且使用也簡單!
#import “UIImageView+AFNetworking.h”

1 [imageView setImageWithURL:[NSURL URLWithString:URL]];

 

AFNetworking 使用總結 (用法+JSON解析)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.