2014-09-24 69 views
0

平淡回应我发送POST请求:获得来自AFNetworking 2.4.1

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 
NSDictionary *parameters = @{@"method": @"login", @"email":@"email", @"password":@"password"}; 
[manager POST:@"http://www.apiwebsite.com" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"JSON: %@", responseObject); 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", error); 
}]; 

我responseObject是:

JSON: <0d0a7b22 72657370 6f6e7365 223a224f 4b222c22 30223a22 6d61696c 696e675f 6b657922 2c223122 3a226239 65653766 34643238 64333730 30666663 37393734 65376665 66626663 3136227d>

但调用API与邮差,我得到:

{"response":"OK", "id":"1234223123"}

如何获取普通或JSON响应以使用它? 谢谢!

回答

0

要获取JSON对象,请使用正确的串行器AFJSONResponseSerializer而不是您现在使用的AFHTTPResponseSerializer

对于调试目的转换NSDataNSString 的NSLog(@ “JSON:%@”,[[ALLOC的NSString] initWithData:responseObject编码:NSUTF8StringEncoding]);

+0

如果我使用''AFJSONResponseSerializer我得到: '错误:错误域= com.alamofire.error.serialization.response代码= -1016 “请求失败:无法接受的内容类型:text/html的” 的UserInfo = 0x8c47a60 ' – pekpon 2014-09-24 16:24:02

+0

@pekpon你设置了'requestSerializer'吗? – sage444 2014-09-25 07:59:45

+0

我可以在哪里设置? – pekpon 2014-09-25 08:19:04