iOS uses AFNetworking to report error Domain=NSCocoaErrorDomain Code=3840, response Code=-1011, http request error 404 solution

Recently, a bug occurred when using AFNetworking to make network requests. It has been solved for a long time. The printed error shows Domain=NSCocoaErrorDomain Code=3840, Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: not found (404) ". When I think about it, why is it 404? The post request is used, and the request code is as follows:

    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:EnergyManagementURL]];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",nil];
    [manager POST:postPath parameters:JSONObjectParameter headers:nil progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if (handler) {
            handler(responseObject);
        }
    } failure:^(NSURLSessionDataTask *task, NSError * error) {
        if (failHandler) {
            failHandler();
        }
    }];

There was no problem in the background. I could get the data by testing with postman, but why did it get 404? So I thought there might be a problem with the manager settings in the upload parameters. But I found that there was no problem. I used AFJSONRequestSerializer and AFJSONResponseSerializer instead of AFHTTPRequestSerializer and AFHTTPResponseSerializer. Later, after comparing the parameters passed in from the front and back multiple times, I finally found the problem. Here’s why:

In the mode of NSJSONWritingPrettyPrinted, the generated substring will have spaces for the sake of appearance, so the length of the compiled data will become longer, and problems will occur in the parsing of the server. The solution is to change this parameter to 0. The specific modifications are as follows:


    AFJSONRequestSerializer *rqSerializer = [AFJSONRequestSerializer serializerWithWritingOptions:0];//NSJSONWritingPrettyPrinted 设置为0
    rqSerializer.stringEncoding = NSUTF8StringEncoding;

    AFJSONResponseSerializer *rsSerializer = [AFJSONResponseSerializer serializer];
    rsSerializer.stringEncoding = NSUTF8StringEncoding; 
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:EnergyManagementURL]];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",nil];
    [manager POST:postPath parameters:JSONObjectParameter headers:nil progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if (handler) {
            handler(responseObject);
        }
    } failure:^(NSURLSessionDataTask *task, NSError * error) {
        if (failHandler) {
            failHandler();
        }
    }];

 

Guess you like

Origin blog.csdn.net/qq_37269542/article/details/108618780