Overview
Afnetworking is a very popular lightweight iOS, MAC OS x network communication class library. Based on Nsurlconnection, Nsoperation and its technology, it has a well-designed modular structure and rich API, making it easy to implement many of the network communication functions.
Afnetworking supports HTTP requests and rest-based network services (including GET, POST, put, delete, and so on). Support Arc. Some column unit tests are also included in the Afnetworking project.
Requires iOS 5.0 and later, or Mac OS 10.7 and later.
Source Address: Https://github.com/AFNetworking/AFNetworking
Practical use
In the source code, has been introduced very clearly, below, for everyone to put out part of the commonly used.
Get method Please ask
No parameter mode:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:
@"http://example.com/resources.json"
parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(
@"JSON: %@"
, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(
@"Error: %@"
, error);
}];
There are parametric ways, in fact, the same as no argument:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSMutableDictionary
*params = [
NSMutableDictionary
dictionary];
params[@
"param1"
] = @
"1"
;
params[@
"param2"
] = @
"2"
;
[manager GET:@
"http://example.com/resources.json"
parameters:params success:^(AFHTTPRequestOperation *operation,
id
responseObject) {
NSLog
(@
"JSON: %@"
, responseObject);
} failure:^(AFHTTPRequestOperation *operation,
NSError
*error) {
NSLog
(@
"Error: %@"
, error);
}];
Post Request mode:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary
*parameters = @{@
"foo"
: @
"bar"
};
[manager POST:@
"http://example.com/resources.json"
parameters:parameters success:^(AFHTTPRequestOperation *operation,
id
responseObject) {
NSLog
(@
"JSON: %@"
, responseObject);
} failure:^(AFHTTPRequestOperation *operation,
NSError
*error) {
NSLog
(@
"Error: %@"
, error);
}];
Also note:
When we request the network, we often see the return data, there is header information, such as:
Content-type:application/json
Afnetworking The default accepted data type is (under Afjsonresponseserializer):
self
.acceptableContentTypes = [
NSSet
setWithObjects:@
"application/json"
, @
"text/json"
, @
"text/javascript"
,
nil
];
If the number of classes returned is Text/plain, an error will occur.
You can add this type directly:
self
.acceptableContentTypes = [
NSSet
setWithObjects:@
"text/plain"
, @
"application/json"
, @
"text/json"
, @
"text/javascript"
,
nil
];
Afnetworking of iOS Web development