今天使用新版AFNetWorking(3.0 beta3) 竟然出错了,错误信息如下:
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0x7fbf4947edf0 {com.alamofire.serialization.response.error.response=...
遇到这种错误不要慌,先看看你post时带的参数params是不是有问题,是服务器不认识的格式?或者是...你字段写错了!仔细检查下你的字段是不是写错了...
Stack Overflow上有位大神这样说:
The error says it all: you got a 400 response from the server, meaning that what you sent was either not formatted properly, or the server just couldn't understand it.
还不行的话,请与你们后台联系..
你可以这样查看返回的中文错误信息:
NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"] ;
NSString *errorStr = [[ NSString alloc ] initWithData:data encoding:NSUTF8StringEncoding];
/**
* 根据code获得一个accessToken(发送一个POST请求)
*
* @param code 授权成功后的请求标记
*/
- (void)accessTokenWithCode:(NSString *)code
{
// 1.获得请求管理者
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// 2.封装请求参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"client_id"] = JXAppKey;
params[@"client_secret"] = JXAppSecret;
params[@"redirect_uri"] = JXRedirectURI;
params[@"grant_type"] = @"authorization_code";
params[@"code"] = code;
// 3.发送post请求
[mgr POST:@"https://api.weibo.com/oauth2/access_token" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *accountDict) {
// 隐藏HUD
[MBProgressHUD hideHUD];
JXLog(@"请求成功--%@", accountDict);
// 字典转成模型
JXAccount *account = [JXAccount accountWithDict:accountDict];
// 存储账号模型
[JXAccountTool saveAccount:account];
// 切换控制器(可能去新特性\tabbar)
[[UIApplication sharedApplication].keyWindow switchRootViewController];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// 隐藏HUD
[MBProgressHUD hideHUD];
JXLog(@"请求失败--%@", error);
}];