objective-c - AFNetworking 2.0 的 POST 请求 - AFHTTPSessionManager

标签 objective-c json post afnetworking afnetworking-2

他,

我正在努力向解析 REST API 发出 POST 请求。我正在使用 AFNetworking 2.0。我的 AFHTTPSessionManager 子类代码如下所示:

+ (ParseAPISession *)sharedSession {
     static ParseAPISession *sharedSession = nil;
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
         sharedSession = [[ParseAPISession alloc] initWithBaseURL:[NSURL URLWithString:kSDFParseAPIBaseURLString]];
    });
   return sharedSession;
}

和:

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (self) {
       [self.requestSerializer setValue:kSDFParseAPIApplicationId forHTTPHeaderField:@"X-Parse-Application-Id"];
       [self.requestSerializer setValue:kSDFParseAPIKey forHTTPHeaderField:@"X-Parse-REST-API-Key"];
   }

   return self;
}

我正在做这样的请求:

[[ParseAPISession sharedSession] POST:@"ClassName" parameters: [NSDictionary dictionaryWithObjectsAndKeys:@"name", @"name", nil] 
                              success:^(NSURLSessionDataTask *task, id abc) {
                                  NSLog(@"%@", abc);
                              } failure:^(NSURLSessionDataTask *task, NSError *error) {
                                  NSLog(@"%@", error);
}];

这样做我总是会遇到这种错误:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8c72420 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

由于 GET 请求就像一个魅力,我很困惑为什么我不能发布一些东西。谁能帮我解决这个问题?

最好的问候!

更新

幸运的是,经过大量测试后,此错误消息不再显示,不幸的是,另一个错误消息出现了:

<NSHTTPURLResponse: 0x8b96d40>
{ URL: https://api.parse.com/1/users }
{ status code: 400, 
headers {
    "Access-Control-Allow-Origin" = "*";
    "Access-Control-Request-Method" = "*";
    "Cache-Control" = "no-cache";
    Connection = "keep-alive";
    "Content-Length" = 130;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Wed, 30 Oct 2013 20:01:58 GMT";
    Server = "nginx/1.4.2";
    "Set-Cookie" = "_parse_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRiIlNjIxZjUxMzY3NWVhZWJmMDYyYWYwMGJiZTQ3MThmMWE%3D--851bd31b07e7dba2c5f83bb13a8d801ecbea42c4; domain=.parse.com; path=/; expires=Fri, 29-Nov-2013 20:01:58 GMT; secure; HttpOnly";
    Status = "400 Bad Request";
    "X-Runtime" = "0.060910";
    "X-UA-Compatible" = "IE=Edge,chrome=1";
} }

任何人都可以告诉我 Status: 400 Bad Request 告诉我什么以及我如何摆脱它?

最佳答案

此错误意味着您的 POST 请求已通过,服务器已成功返回一些数据,NSJSONSerialization 解析时遇到问题。

您可能需要设置您的 AFJSONResponseSerializer 以允许 JSON 片段。

AFHTTPSessionManager 子类的 init 方法中:

AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[self setResponseSerializer:responseSerializer];

如果这不起作用,您可能遇到了编码问题。来自 NSJSONSerialization 类引用:

The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM. The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8.

检查服务器发送的编码类型。

最后,您可以在 AFNetworking 内部设置断点,或者设置 AFNetworkActivityLogger ,这将记录请求发送和接收到您的控制台。此工具对于调试此类问题非常有用。

关于objective-c - AFNetworking 2.0 的 POST 请求 - AFHTTPSessionManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19680193/

相关文章:

ios - iPhone 应用程序 PayPal 集成

ios - 使用Codable解析嵌套JSON数据问题

java - 在java Hashmap中使用JSON对象

python - 从 Python 脚本使用 POST 发送文件

c# - 来自代码隐藏的 Api POST

ios - Google Places API 搜索结果在 map 上刷新

ios - 单击下一个和上一个UIButton时在UILabel中更改日期

ios - 在 AFNetworking 2.x 中替换 AFJSONRequestOperation

node.js - JSONSerialization 生成不正确的 JSON 对象。 swift

javascript - 使用 React 和 redux 的 Post 方法