如何使用 AFNetworking 2.0 POST 数据? [英] How to POST data using AFNetworking 2.0?

查看:18
本文介绍了如何使用 AFNetworking 2.0 POST 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过 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(即具有 application/x-www-form-urlencoded<Content-Type 的请求/code>).

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 POST 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆