[IOS] network operations and AFNetworking, iosafnetworking
As we all know, the NSContention framework developed by Apple is cumbersome in sending and receiving requests. This operation is inconvenient. Not onlySet different parameters for different requestsAnd oftenMulti-thread operationsAnd the request and returned dataVarious serialized operationsAnd also consider the requestData securityAnd other issues.
I. Earlier network frameworks1. ASI framework: HTTP Terminator. Very good, but there is a BUG that has stopped updating. 2. MKNetworkKit (written by Indians). 3. AFN is still being updated.
AFNetworking: a network framework designed for MAC/iOS (to simplify network operations)
Address: https://github.com/AFNetworking/AFNetworking
* AFN focuses on network data transmission and multi-thread processing in the network.
Ii. Use of AFNetworking
1. AFN features: * When you log on to the system to transmit parameters, you can pass the dictionary. (The key name is the parameter name, and the key value is the parameter value ). * It is automatically executed in the Child thread. After execution, it returns to the main thread. * The returned results are automatically serialized as NSDictionary.
2. Use AFN. Note: * AFHTTPRequestOperationManager encapsulates common methods for communication with Web applications over HTTP. (this is not a singleton during instantiation because there is no shared word.) * includes creating requests/responding to serialization/network monitoring/data security. * methods and so on all begin with AF.
3. What AFN can do (all in the Network): * GET/POST/PUT/DELETE/HEAD requests. * JSON data parsing/Plist data parsing. (XML data parsing is not supported) * POSTJSON. * upload/download.
4. Usage steps: (refer to the instructions) 1. first, you need to instantiate a request manager AFHTTPRequestOperationManager.2. set the request data format: the default value is binary. (not modifiable) * AFHTTPRequestSerializer (Binary) * AFJSONRequestSerializer (JSON) * AFPropertyListRequestSerializer (Plist) 3. set the response data format. The default format is JSON. (not changeable) * AFHTTPResponseSerializer (Binary) * AFJSONResponseSerializer (JSON) * Merge (Plist) * AFXMLParserResponseSerializer (XML) * AFImageResponseSerializer (Image) * combine (combined) 4. if the MIMEType of the responder is incorrect, modify acceptableContentTypes.5. call the method and send the Response Request (GET/POST ...).
About modifying the AFN source code: You can modify the source code once and for all to support text/plan during serialization. You can modify the source code in acceptableContentTypes.
AFN for GET and POST login:
# Pragma mark-get/post login-(void) getLogin {// 1. manager AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager]; // 2. set the logon parameter NSDictionary * dict ={ @ "username": @ "xn", @ "password": @ "123"}; // 3. request [manager GET: @ "http: // localhost/login. php "parameters: dict success: ^ (AFHTTPRequestOperation * operation, id responseObject) {NSLog (@" GET --> % @, % @ ", responseObject, [NSThread currentThread]); // automatically return to the main thread} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {NSLog (@ "% @", error);}];} /*** is exactly the same as the GET method above. Only one POST parameter is different */-(void) postLogin {// 1. manager AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager]; // 2. set the logon parameter NSDictionary * dict ={ @ "username": @ "xn", @ "password": @ "123"}; // 3. request [manager POST: @ "http: // localhost/login. php "parameters: dict success: ^ (AFHTTPRequestOperation * operation, id responseObject) {NSLog (@" POST --> % @, % @ ", responseObject, [NSThread currentThread]); // automatically return to the main thread} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {NSLog (@ "% @", error);}];}
AFN parses network data to obtain Plist, JSON, and XML(
AFN does not support automatic XML ParsingHas a dedicated framework, such as SAX, PULL, and KissXML)
# Pragma mark-get data parsing-(void) getJSON {// 1. request manager AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager]; // 2. initiate the request [manager GET: @ "http: // localhost/videos. json "parameters: nil success: ^ (AFHTTPRequestOperation * operation, id responseObject) {NSLog (@" % @ ", responseObject);} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {NSLog (@ "% @", error) ;}] ;}/ *** XML data parsing not supported */-(void) getXML {// 1. manager AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager]; // 2. set the returned Data Type manager. responseSerializer = [AFXMLParserResponseSerializer serializer]; // instantiate it first // 3. initiate the request [manager GET: @ "http: // localhost/videos. xml "parameters: nil success: ^ (AFHTTPRequestOperation * operation, id responseObject) {NSLog (@" % @ ", responseObject);} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {NSLog (@ "% @", error) ;}] ;}- (void) getPlist {// 1. manager AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager]; // 2. set the response type manager. responseSerializer = [AFPropertyListResponseSerializer serializer]; // It is Response. Do not write it as request. change to plist type. manager. responseSerializer. acceptableContentTypes = [NSSet setWithObject: @ "text/plain"]; // This can be directly modified in the framework. | search for xuneng in the project and find the positioning point. // 3. request [manager GET: @ "http: // localhost/videos. plist "parameters: nil success: ^ (AFHTTPRequestOperation * operation, id responseObject) {NSLog (@" % @ ", responseObject);} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {NSLog (@ "% @", error) ;}];}
Post json data with AFN, upload, download, etc.. (Https://github.com/AFNetworking/AFNetworking is available on the upload/download home notes)
# Pragma mark-post json data and uploaded files-(void) postJSON {// 1. manager AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager]; // 2. set the type. (set the request-response type here) manager. requestSerializer = [AFJSONRequestSerializer serializer]; manager. responseSerializer = [AFHTTPResponseSerializer serializer]; // This determines the type returned by the following responseObject // manager. responseSerializer = [AFJSONResponseSerializer serializer]; // manager. responseSerializer. acceptableContentTypes = [NSSet setWithObject: @ "text/plain"]; // 2. set the logon parameter NSDictionary * dict ={ @ "username": @ "xn", @ "password": @ "123"}; // 3. send the request [manager POST: @ "http: // localhost/postjson. php "parameters: dict success: ^ (AFHTTPRequestOperation * operation, id responseObject) {// NSLog (@" postjson -- >%@ ", responseObject ); // set text/plain NSString * result = [[NSString alloc] initWithData: responseObject encoding: NSUTF8StringEncoding]; NSLog (@ "% @", result );} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {NSLog (@ "% @", error) ;}];}
Reprinted please indicate the source: http://blog.csdn.net/xn4545945
How does iOS implement network programming?
You can use the CFNetwork framework and CFSocket socket to create UDP and TCP connections, send data, and set callback functions.
IOS AFNetworking Problems
It is clear that the second object of each pair must be non-empty, or you forget the last nil in the parameter list. Check whether the data submitted by post has a null value, you can only see so much. Print your body before sending the request to see if there is any value missing, or simulate some static data.