我正在使用AFNetworking
进行Web服务。它在NSURLConnection
上工作正常。它使用AFNetworking
提供错误。
这是我的代码:
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:@"" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"Succes");
} failure:^(NSURLSessionDataTask *task, NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
这是我收到的错误:
Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: method not allowed (405)" UserInfo=0x1e86b390 {NSErrorFailingURLKey=http://..............url.........., AFNetworkingOperationFailingURLResponseErrorKey=, NSLocalizedDescription=Request failed: method not allowed (405)}.
你能帮忙吗?
答案 0 :(得分:2)
如果您的API是RestFul,那么您想要使用http请求。使用下面的代码可能对您有所帮助。请告诉我们您的意见。
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
NSString *post =[dict JSONRepresentation];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%ld",(long)[postData length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
//Add your request object to an AFHTTPRequestOperation
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request] ;
[operation setCompletionBlockWithSuccess: ^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *response = [operation responseString];
NSLog(@"response: %@",response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", [operation error]);
}];
//call start on your request operation
[operation start];