我試圖使用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被拋出?我可以導航到它試圖擊中的網址,但它說網頁不可用
在此先感謝!
這不是默認的響應串行器嗎?它不應該已經在使用這個? – Minimi
我不知道它是否是默認值。我有一個類似的錯誤,然後我碰到一個SO帖子,他們建議我添加這一行。我遇到的錯誤是:「錯誤:錯誤域= com.alamofire.error.serialization.response代碼= -1016」請求失敗:不可接受的內容類型:text/html「」 – Alex
所以事實證明,默認是json串行器。但總的來說,我認爲我對它的看法是錯誤的。從我設置的服務器返回的響應都是JSON,所以我應該使用默認值。我的問題是我沒有觸碰端點,所以返回的響應是HTML,所以它拋出了兩個錯誤,一個是我們未能擊中端點(404),另一個是它無法解析text/html。第二個被拋出的原因是因爲我沒有碰到端點,所以修復這個問題修復了其他問題。 – Minimi