如何使用AFNetworking 2.0发布数据? [英] How to POST data using AFNetworking 2.0?
问题描述
我需要通过POST以可变数据发送我的数据。
I need to send my data by POST, in variable data.
我是这样做的:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"email" : email, @"password" : pass };
[manager POST:URLString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:
^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error); }];
并且:
JSON: {
Data = "<null>";
Message = "unexpected end of JSON input";
Result = fail;
}
我知道方法
- (AFHTTPRequestOperation *)POST:(NSString *)URLString
parameters:(NSDictionary *)parameters
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData>))block
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure;
但是如何将可变数据中的字典发送到网络服务器?
But how can I send my dictionary in variable data to the web server?
推荐答案
您的Web服务是否希望将参数格式化为JSON?如果是这样,在调用 POST
方法之前,您需要告诉 manager
使用JSON requestSerializer
,即 AFJSONRequestSerializer
:
Does your web service expect the parameters to be formatted as JSON? If so, before you invoke the POST
method, you need to tell the manager
to use a JSON requestSerializer
, i.e. AFJSONRequestSerializer
:
manager.requestSerializer = [AFJSONRequestSerializer serializer];
默认情况下,AFNetworking假设您要使用 AFHTTPRequestSerializer
(即 Content-Type
application / x-www-form-urlencoded
的请求)。
By default, AFNetworking assumes you want to use AFHTTPRequestSerializer
(i.e. a request with a Content-Type
of application/x-www-form-urlencoded
).
这篇关于如何使用AFNetworking 2.0发布数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!