来自GET请求的AFNetworking 3.0 404响应
我试图使用AFNetowrking进行GET请求。下面的代码:来自GET请求的AFNetworking 3.0 404响应
@property (nonatomic, readwrite) AFHTTPSessionManager *httpSessionManager;
...
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
_httpSessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:self.baseURL]
sessionConfiguration:config];
_httpSessionManager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
_httpSessionManager.responseSerializer.acceptableContentTypes = [_httpSessionManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
_httpSessionManager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
_httpSessionManager.securityPolicy.allowInvalidCertificates = YES;
...
NSMutableDictionary* parameters = [self defaultUserSessionParameters];
NSString *url = [self constructURLWithEndpointPath:@"lead_sources.json"];
RequestSuccessBlock winBlock = ^(NSURLSessionDataTask *task, id responseObject)
{
NSLog(@"GetLeadSources, response Object: %@", [responseObject description]);
NSArray* leadSources = responseObject[@"lead_sources"];
if (!leadSources)
{
NSString* errorString = @"Successfully retrieved lead sources but no values were returned!";
NSLog(@"%@",errorString);
NSError* error = [NSError bpdErrorWithDescription:errorString];
completion(nil, error);
}
else
{
completion(leadSources,nil);
}
};
RequestFailureBlock failureBlock = ^(NSURLSessionDataTask *task, NSError *error)
{
NSLog(@"FAILURE: get lead sources, Error: %@", error);
NSString* ErrorResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
NSLog(@"server error response: %@ /end", ErrorResponse);
};
[self.httpSessionManager GET:url
parameters:parameters
progress:nil
success:winBlock
failure:failureBlock];
但每次我得到这个错误:
Request failed: unacceptable content-type: text/html
我做了一些挖掘,发现了这个错误,是因为我的反应序列化的抛出,所以我说这行修复问题:
_httpSessionManager.responseSerializer.acceptableContentTypes = [_httpSessionManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
但当我添加此我得到以下错误:
JSON text did not start with array or object and option to allow fragments not set.
为此我发现我需要设置阅读选项以允许片段。我试着串行设置这样:
_httpSessionManager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
但后来我得到另一个错误:
Invalid value around character 0
我想我必须设置响应序列化是一个AFHTTPResponseSerializer,但我似乎无法图了解如何允许使用该序列化器的片段。
无论哪种方式,我不认为这就是404被抛出的原因。我相信我没有击中我尝试击中的域名,而AFNetworking在解析由我所击中的URL引发的响应时遇到了问题。
所以两个问题:
如何让这个我的反应由串行正确解析? (aka沉默这三个错误?)
为什么404被抛出?我可以导航到它试图击中的网址,但它说网页不可用
在此先感谢!
从评论:
因此,原来默认的是JSON序列。但总的来说,我认为我对它的看法是错误的。从我设置的服务器返回的响应都是JSON,所以我应该使用默认值。我的问题是我没有触碰端点,所以返回的响应是HTML,所以它抛出了两个错误,一个是我们未能击中端点(404),另一个是它无法解析text/html。第二个被抛出的原因是因为我没有碰到端点,所以修复这个问题修复了其他问题。
您可以按如下方式将响应序列化程序设置为AFHTTPResponseSerializer。但是,如果错误404是由于资源不在您要提交的路径url中,唯一的解决方案是找出正确的URL。
_httpSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
这不是默认的响应串行器吗?它不应该已经在使用这个? – Minimi
我不知道它是否是默认值。我有一个类似的错误,然后我碰到一个SO帖子,他们建议我添加这一行。我遇到的错误是:“错误:错误域= com.alamofire.error.serialization.response代码= -1016”请求失败:不可接受的内容类型:text/html“” – Alex
所以事实证明,默认是json串行器。但总的来说,我认为我对它的看法是错误的。从我设置的服务器返回的响应都是JSON,所以我应该使用默认值。我的问题是我没有触碰端点,所以返回的响应是HTML,所以它抛出了两个错误,一个是我们未能击中端点(404),另一个是它无法解析text/html。第二个被抛出的原因是因为我没有碰到端点,所以修复这个问题修复了其他问题。 – Minimi