Ios 坚持使用AFN网络

Ios 坚持使用AFN网络,ios,afnetworking,Ios,Afnetworking,我无法找到我的代码的真正问题。我对所有其他API都使用相同的代码,而且一切正常,但有一个API出现了问题 NSString *strUrl = [[NSString stringWithFormat:@"%@%@",baseURL,apiURL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSMutableURLRequest *request = [NSMutableURLRequest r

我无法找到我的代码的真正问题。我对所有其他API都使用相同的代码,而且一切正常,但有一个API出现了问题

    NSString *strUrl = [[NSString stringWithFormat:@"%@%@",baseURL,apiURL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strUrl] cachePolicy:NSURLRequestReloadIgnoringCacheData  timeoutInterval:30.0];

    //request type may be "POST" or "GET"
    [request setHTTPMethod:requestType];
    [request setValue:secretKey forHTTPHeaderField:key];

    //contentType may be "application/json" or "text/html"
    [request setValue:contentType forHTTPHeaderField:@"Accept"];
    [request setValue:contentType forHTTPHeaderField:@"Content-Type"];

    AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    requestOperation.responseSerializer = [AFJSONResponseSerializer serializer];

    [requestOperation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
        completion(nil);
    }];

    [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        completion(nil);
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        completion(nil);
    }];
此代码适用于除一个API之外的所有API。它在错误块中给了我这个错误

Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html"
JSON text did not start with array or object and option to allow fragments not set.
然后,我尝试通过这样做添加可接受的内容类型

requestOperation.responseSerializer.acceptableContentTypes = [requestOperation.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
这不起作用。然后,我将响应序列化程序更改为AFHTTPResponseSerializer,并删除了可接受的内容类型行。然后,响应以NSData的形式出现,我将NSData响应解析为所有其他api的JSON对象。但此API在更改响应序列化程序后出现此错误。这是来自错误块的错误

Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html"
JSON text did not start with array or object and option to allow fragments not set.

我不明白为什么会这样。我尝试将标题可接受的内容类型更改为json和html。但这些都没有任何区别。我在Chrome Postman中试用了API,它运行得非常好。请提出我做错的地方。

将内容类型设置为application/Json,然后重试


[请求设置值:@“应用程序/json”用于HttpHeaderField:@“内容类型”]

当我尝试与邮递员的json文本是完全格式化,我没有得到格式错误。但当我尝试使用AFN网络时,它出现了错误块。答案是否定的,请检查我的问题。首先,我使用AFJSONRequestOperation,但它给出的错误是可接受的内容类型是text/html,我通过在stack和github中搜索发现,如果我使用AFHTTPResponseSerializer,那么这个错误将得到解决。但这又引发了另一个错误。