New iOS users learn AFNetworking (1), iosafnetworking

Source: Internet
Author: User

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);}];

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.