iOS AFnetworking post请求 直接post上去一个json格式的数据 报Domain=com.alamofire.error.serialization.response Code=

这两天调接口,后台让使用post请求,直接post上去一个json串,有token验证,整了好久,没有调成功,先上原来旧代码:

   NSMutableDictionary * parameters = [NSMutableDictionary dictionaryWithCapacity:1];

/*

*参数在这里省去

/

    AFHTTPSessionManager* sessionManager = [AFHTTPSessionManager manager];
    sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
    sessionManager.requestSerializer.timeoutInterval = requestTime;  //设置请求超时时间
    sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
 
    [sessionManager POST:[NSString stringWithFormat:@"%@api/v1/dog/dognewborn",URL_NEWGAME] parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
    
        NSLog(@"%@",responseObject);
        NSLog(@"mag = %@",[responseObject objectForKey:@"msg"]);
    
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(task,error);
        NSLog(@"error= %@",error);

        long flag = error.code;
        switch (flag) {
            case -1009:
                [MBProgressHUD showError:@"网络好像连接不上了,请重试"];
                break;
            case -1001:
                [MBProgressHUD showError:@"请求超时,请稍后重试 "];
                break;
                
            default:
                break;
        }

    }];

结果报以下错误:

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unauthorized (401)" UserInfo={com.alamofire.serialization.response.error.response= { URL: http://****************/dognewborn } { status code: 401, headers {
    "Content-Length" = 0;
    Server = "Jetty(7.6.14.v20131031)";
} }, NSErrorFailingURLKey=http://******************/dognewborn, NSLocalizedDescription=Request failed: unauthorized (401), com.alamofire.serialization.response.error.data=<>}

401错误,网上提示说是没有权限访问该接口,甚是郁闷,看了安卓那边,发现他们调的接口,需要把token拼接到url后面即:[NSString stringWithFormat:@"%@api/v1/dog/dognewborn?token=%@",URL_NEWGAME,token]
安卓的网络请求框架是自动把token添加到接口后面,af需要自己拼上去,才能请求。改正后代码如下:

   NSMutableDictionary * parameters = [NSMutableDictionary dictionaryWithCapacity:1];


/*

*参数在这里省去

/


    AFHTTPSessionManager* sessionManager = [AFHTTPSessionManager manager];
    sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
    sessionManager.requestSerializer.timeoutInterval = requestTime;  //设置请求超时时间
    sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
 
    [sessionManager POST:[NSString stringWithFormat:@"%@api/v1/dog/dognewborn?token=%@",URL_NEWGAME,token] parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
    
        NSLog(@"%@",responseObject);
        NSLog(@"mag = %@",[responseObject objectForKey:@"msg"]);
    
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(task,error);
        NSLog(@"error= %@",error);

        long flag = error.code;
        switch (flag) {
            case -1009:
                [MBProgressHUD showError:@"网络好像连接不上了,请重试"];
                break;
            case -1001:
                [MBProgressHUD showError:@"请求超时,请稍后重试 "];
                break;
                
            default:
                break;
        }

    }];

最后请求成功,问题解决。





你可能感兴趣的:(IOS)