afnetworking method of Use (2.0)
This article describes the AFNetworking-2.0 usage method (add adaptation: Content format of meta in Imperfect head)
As the asihttprequest stopped updating, many people turned to afnetworking and Mknetworkkit. I'm one of them. So I looked for a lot of articles from the Internet for reference, but the result is a failure. Study for a long time can not get through, and finally asked someone to help fix it. Often from the online request for free information, to have the idea of return, but also in order to let more people take a few detours, so the following is the code: (There are errors can be pointed out)
First: Add afnetworking, uikit+afnetworking to the project
Generally refer to these two packages, or there will be error prompts: Systemconfiguration.framework, mobilecoreservices.framework
And where to use it.
#import "AFHTTPRequestOperationManager.h"
#import "AFHTTPSessionManager.h"
Afhttprequestoperationmanager Post has two methods, one is the normal post, the other is the image can be uploaded
1. Upload Image:
Afhttprequestoperationmanager *manager = [Afhttprequestoperationmanagermanager]; Manager.responseSerializer.acceptableContentTypes = [nssetsetwithobject:@ "text/html"];
Nsdictionary *parameters [Email protected]{@ "Parameter 1": @ "value1", @ "Parameter 2":
@ "value2" 、、、};
NSData *imagedata = uiimagejpegrepresentation ([UIImage imagenamed:@ "1.png"], 1.0);
[Manager post:@ "is replaced 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 can upload pictures, if not upload pictures, you can remove this sentence
[FormData appendpartwithfiledata:imagedata
name:@ "1"
filename:@ "1.png"
mimetype:@ "Image/jpeg"]
2. Normal Post
Afhttprequestoperationmanager *manager = [Afhttprequestoperationmanagermanager];
Manager.responseSerializer.acceptableContentTypes = [nssetsetwithobject:@ "text/html"];
Nsdictionary *parameters =@{@ "Parameter 1":@ "value1",@ "Parameter 2": @ "value2" 、、、};
[Managerpost:@ "replaced 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);
}];
3. Add adaptation: Content format of meta in incomplete head (March 17, 2014 11:20) ref: Http://www.cocoachina.com/bbs/simple/?t176000.html
| tom19830924 |
2013-12-27 17:44 |
This is the second time I've answered this question. This is not afnetworking's problem. This is the person who does the server. The content format of meta in head is not specified.
Please take a test. Http://stackoverflow.com/questions/477816/what-is-the-correct-json-content-type |
| Evangel |
2014-03-18 21:41 |
Afhttprequestoperationmanager *manager = [Afhttprequestoperationmanager manager]; Manager.requestserializer = [Afjsonrequestserializer serializer]; Manager.responseserializer = [Afjsonresponseserializer serializer]; [Manager.requestserializer setvalue:@ "Application/json" forhttpheaderfield:@ "Accept"]; [Manager.requestserializer setvalue:@ "Application/json; charset=utf-8" forhttpheaderfield:@ "Content-Type"]; |
Workaround: I am using a
Evangel method, add the following 4 sentences:
Manager.requestserializer = [Afjsonrequestserializer serializer];
Manager.responseserializer = [Afjsonresponseserializer serializer];
[Manager.requestserializer setvalue:@ "Application/json" forhttpheaderfield:@ "Accept"];
[Manager.requestserializer setvalue:@ "Application/json; charset=utf-8" forhttpheaderfield:@ "Content-Type"];
The following is the complete request setting:
Afhttprequestoperationmanager *manager = [Afhttprequestoperationmanagermanager];
Manager.responseserializer. Acceptablecontenttypes = [nssetsetwithobject:@ "text/html" ];
Manager. Requestserializer = [Afjsonrequestserializerserializer];
Manager. Responseserializer = [Afjsonresponseserializerserializer];
[Manager. requestserializersetValue: @ "Application/json"Forhttpheaderfield: @ "Accept"];
[Manager. requestserializersetValue: @ "Application/json; charset=utf-8"Forhttpheaderfield: @ " Content-type "];
4. Timeout setting (October 17, 2014 15:20) afnetworking 2.0 has no time-out setting, as if 2.1 has a timeout setting. Open the version you downloaded, find afurlrequewtserialization.m, search for timeout. Timeout default time is 60 seconds, change it to the time you need
The most common use of afnetworking is post and get requests, but many people may not know that this powerful framework can also be configured Httpheader
First look at the methods we used
| 1234567 |
afhttprequestoperationmanager *manager = [Afhttprequestoperationmanager manager]; manager.responseserializer = [afjsonresponseserializer new [manager post:url parameters:parameters success:^ (afhttprequestoperation *operation, id responseobject) { failure:^ (afhttprequestoperation *operation, nserror *error) { }]; |
We only need to give the manager a Responseserializer new object for this routine usage, but if you need to configure a specific header for the request, you can look at the following code
First, to the request of the serializer new object, this step must not forget, or the subsequent configuration is invalid!!!
| 1 |
manager.requestSerializer = [AFJSONRequestSerializer new]; |
Set the type of request content
| 1 |
[manager.requestSerializer setValue:@"application/json;charset=utf-8"forHTTPHeaderField:@"Content-Type"]; |
Set the length of the requested content
| 1 |
[manager.requestSerializer setValue:[NSStringstringWithFormat:@"%ld", (unsigned long)[jsonStr length]] forHTTPHeaderField:@"Content-Length"]; |
Set the encoding type of the request
| 1 |
[manager.requestSerializer setValue:@"gzip"forHTTPHeaderField:@"Content-Encoding"]; |
Afnetworking method of Request