New iOS users learn AFNetworking (1), iosafnetworking
Source code download: http://download.csdn.net/detail/haogaoming123/8555077
As asihttprequest stops updating, many people turn to AFNetworking and MKNetworkKit. I am also one of them. So I found a lot of articles on the Internet for reference, but the results all failed. After studying for a long time, I couldn't figure it out. I finally asked someone for help. A member who frequently asks for free information from the Internet should have the idea of returning the information and make more and more people detour, so the following code is: (if there is an error, you can point it out) first: add AFNetworking, UIKit + AFNetworking to the project and # import "AFHTTPRequestOperationManager" to be used. h "# import" AFHTTPSessionManager. h "AFHTTPRequestOperationManager has two methods for post. One is normal post, and the other is the one that can upload images. upload image: AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManagermanager]; manager. responseSerializer. acceptableContentTypes = [NSSetsetWithObject: @ "text/html"]; NSDictionary * parameters = @ {@ "parameter 1": @ "value1", @ "parameter 2 ": @ "value2",}; NSData * imageData = UIImageJPEGRepresentation ([UIImage imageNamed: @ "1.png"], 1.0); [manager POST: @ "Replace with the address you want to access" parameters: parametersconstructingBodyWithBlock: ^ (id <AFMultipartFormData> formData) {[formData appendPartWithFileData: imageData name: @ "1" fileName: @ "1.png" mimeType: @" image/jpeg "];} success: ^ (AFHTTPRequestOperation * operation, id responseObject) {NSLog (@" Success: % @ ", responseObject );} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {NSLog (@ "Error: % @", error) ;}]; this method uploads images. If you do not need to upload images, you can remove this sentence from [formData appendPartWithFileData: imageDataname: @ "1" fileName: @ "1.png" mimeType: @" image/jpeg "] 2. common postAFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManagermanager]; manager. responseSerializer. acceptableContentTypes = [NSSetsetWithObject: @ "text/html"]; NSDictionary * parameters = @ {@ "parameter 1": @ "value1", @ "parameter 2 ": @ "value2",}; [managerPOST: @ "Replace with the address you want to access" parameters: parameters success: ^ (AFHTTPRequestOperation * operation, id responseObject) {NSLog (@ "Success: % @", responseObject);} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {NSLog (@ "Error: % @", error);}];