- Afnetwork is a lightweight network Request API Class library. is based on Nsurlconnection, Nsoperation and other methods.
- The following example is used to process the JSON request
3 How to choose the afnetworking version
The first thing you need to do is download the afnetworking library file, and when you download it you have to figure out what the minimum version of the software you are going to develop is. Afnetworking 2.0 or later versions require a xcode5.0 version and can only be run on a mobile system that is IOS6 or higher, and if you develop a Mac program, then version 2.0 will only run on Mac OS X 10.8 or higher.
Afnetworking 2.0 https://github.com/AFNetworking/AFNetworking If you want to be compatible with IOS5 or Mac OS X 10.7, then you need to use the latest release of the 1.x version afnetworking 1.x https://github.com/AFNetworking/AFNetworking/tree/1.x If you want to be compatible with 4.3 or Mac OS X 10.6, you need to use the latest released 0.10.x version afnetworking 0.10.xhttps://github.com/afnetworking/afnetworking/tree/0.10.x official website Download version 2.5: http://afnetworking.com/ 2013 most software compatible minimum version is 4.3, and 2014, most of the estimated compatibility of the minimum version will be 5.0 or even 6.0, so the best choice is the 1.x version, compatible to IOS5.0.
Next is the header file to be introduced
#import "AFNetworking.h"
And then
-(void) Viewdidload {
NSString *urlstring = [NSString stringwithformat:@ "Http://baobab.wandoujia.com/api/v1/feed.bak?num=10&date=%f" , [self getdate]];
Nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:urlstring];
Afhttprequestoperation *operation = [[Afhttprequestoperation alloc]initwithrequest:request];
[Operation setcompletionblockwithsuccess:^ (afhttprequestoperation *operation, id responseobject) {
NSString *html = operation.responsestring;
nsdata* data=[html datausingencoding:nsutf8stringencoding];
Nsdictionary *dict = [nsjsonserialization jsonobjectwithdata:data options:0 Error:nil];
The interpretation of their own words here to start to parse their own content can be
Self.dataarr = [Nsmutablearray new];
Self.videoarray = [Nsmutablearray new];
For (Nsdictionary *dailylist in dict[@ "Dailylist"])
{
NSString *key = dailylist[@ "Date"];
Nsmutablearray *temparr = [Nsmutablearray new];
For (Nsdictionary *videomodel in dailylist[@ "Videolist"]) {
Fzmenumodel *model = [[Fzmenumodel alloc]init];
[Model Setvaluesforkeyswithdictionary:videomodel];
[Temparr Addobject:model];
}
[Self.keysarr Addobject:key];
[Self.datadit Setvalue:temparr Forkey:key];
}
[Self.keysarr sortusingselector: @selector (compare:)];
End parsing
Refresh (a view that is defined in the controller itself, and a tableview defined in the view defined by itself)
[Self.fzmenuview.maintableview Reloaddata];
NSLog (@ "%@", dict);
}failure:^ (afhttprequestoperation *operation, Nserror *error) {
NSLog (@ "error occurred!) %@ ", error);
}];
Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];
[Queue addoperation:operation];
}
Time for the above URL call
-(Nstimeinterval) getdate{
nsdate* dat = [nsdate datewithtimeintervalsincenow:0];
Nstimeinterval a = [dat timeintervalsince1970]* 1000;
return A;
}
Afnetworking Basic Use