我正在使用AFNetworking 2.0。 我将我的单例类实现为:AFHTTPSessionManager
的子类+ (RESTClientManager *)sharedManager
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:@"http://myurl.com:8080"]];
});
return _sharedInstance;
}
- (instancetype)initWithBaseURL:(NSURL *)url
{
self = [super initWithBaseURL:url];
if (self) {
self.responseSerializer = [AFJSONResponseSerializer serializer];
self.requestSerializer = [AFJSONRequestSerializer serializer];
}
return self;
}
当我需要发布POST时,我使用以下代码:
[self POST:@"/users" parameters:requestParameters success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"Ok");
}
failure:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"Failed");
}];
但我得到了错误:
{ status code: 406, headers {
Connection = "keep-alive";
"Content-Length" = 0;
"Content-Type" = "application/json";
Date = "Sat, 30 Aug 2014 09:29:15 GMT";
Server = "8";
"X-Powered-By" = "Undertow 1";
} }, NSLocalizedDescription=Request failed: unacceptable (406)}
使用RESTful客户端我尝试发出相同的请求: 当我添加此标题字段时,这很有效:
- 接受":application / json
- Content-Type:application / json
如何使用AFNetworking 2.0?
修改
打印参数字典
po requestParameters
{
city = "";
country = Italy;
deviceToken = "";
email = "email@sdsd.it";
mobileNumber = "";
name = "Test_iOS";
phoneNumber = "";
postalCode = "";
street = "";
streetNumber = "";
surname = "";
userId = 00000001;
}
答案 0 :(得分:0)
如果您在请求标头中添加了正确的Content-Type
,那么您的参数可能就是错误的来源。如果您可以联系负责后端服务器的任何人,您可以从他们那里获得准确的错误报告。如果没有,请确保参数不为空,并且您传递的内容遵循服务器使用的任何标准。
如果我们能看到传递的参数,我们可以帮助你。