我正在將我的一些代碼從AFNetworking 1.0切換到2.0。AFNetworking 2.0 POST問題
做一個POST時之前,我是創造一個AFHTTPClient
和AFHTTPRequestOperation
像這樣:
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:reqUrl];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
httpClient.operationQueue.maxConcurrentOperationCount = 1;
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
req.viewName, @"viewName",
req.json, @"JSON",
req.dateAdded.description, @"dateTime",
req.latitude, @"latitude",
req.longitude, @"longitude",
req.heading, @"heading",
req.user, @"requestUser",
nil];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[op setCompletionBlockWithSuccess:
^(AFHTTPRequestOperation *operation,
id responseObject) {
.......convert responseObject (string) to NSDictionary.....
});
這工作得很好,和我的職位經歷,我從服務器接收到一個成功的文本響應。 (然後我轉換爲NSDictionary
)
我現在正在使用AFHTTPSessionManager
singleton,並從中調用POST方法。當初始化我AFHTTPSessionManager
,我做了以下內容:
AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
[self setResponseSerializer:responseSerializer];
self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
然後在我的其他類,我打電話的POST像這樣:
NSDictionary *params = @{
@"viewName":req.viewName,
@"JSON":req.json,
@"dateTime":req.dateAdded.description,
@"latitude":req.latitude,
@"longitude":req.longitude,
@"heading":req.heading,
@"requestUser":req.user
};
[netManager POST:path parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
.....
} failure:^(NSURLSessionDataTask *task, NSError *error) {
//failing here
});
我的數據還沒有改變,但職位總是失敗,出現錯誤:
Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: bad request (400)" UserInfo=0x1704675c0 {AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x178234660> { URL: ... } { status code: 400, headers {
"Content-Length" = 2738;
"Content-Type" = "text/html";
Date = "Thu, 15 May 2014 16:13:51 GMT";
Server = "Microsoft-IIS/7.0";
"X-Powered-By" = "ASP.NET";
請告訴我不同,是造成新的AFNetworking 2.0 POST代碼到與這個工作嗎?有什麼我需要設置?我傳遞的URL和參數與我發送POST的舊方式相同。
感謝
順便說一句,前兩行是多餘的,因爲它們是默認值。 –
哇,我正在試圖努力讓這個工作!我只需要設置RequestSerializer -.- Ya代碼並學習。 – RyanG