2013-10-30 96 views
6

Hej,带AFNetworking 2.0的POST请求 - AFHTTPSessionManager

我很努力地对分析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请求的作用像一个魅力,我很困惑为什么我不能发布一些东西。有人可以用这个问题来遏制我吗?

此致敬礼!

UPDATE

周围有很多了,不再显示此错误消息经过测试令人高兴的是,unfortuatelly另一个出现了:

<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"; 
} } 

谁能告诉我的状态是什么:400错误的请求告诉我和我如何摆脱它?

回答

9

此错误表示您的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,这将在请求发送和接收到您的控制台时记录请求。这个工具对调试这类问题非常有帮助。

+0

(也有可能在服务器返回比JSON以外的东西,在这种情况下,你不'不想使用'AFJSONResponseSerializer') –

+0

Hej,谢谢你的回应!我尝试了这一点,但在我得到另一种错误消息的时刻(见上面的更新),我认为原来的问题不再是主要问题....也许你可以帮助我呢? – RuNsTa

+0

你应该为此询问一个单独的问题。您的请求不是Parse的正确格式,服务器拒绝它。 (IE,你现在有一个解析问题,而不是一个AFNetworking问题。) –

1

这为我工作:

AFHTTPSessionManager

与下列串行器初始化它:

[self setRequestSerializer:[AFHTTPRequestSerializer serializer]]; [self setResponseSerializer:[AFJSONResponseSerializer serializer]];