问题
So I want to use AFNetworking V2.0 for a GET request with NSURLSession (iOS7's new API)
So far I have got this - but is this the correct way to do it?
NSString *tempURL =[NSString stringWithString:url];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:tempURL parameters:params success:^(NSURLSessionDataTask *task, id responseObject)
{
//Working
}failure:^(NSURLSessionDataTask *task, NSError *error)
{
//Failed!!
}];
Would this be the correct way to do it?
回答1:
I think what you are missing is an alloc-init
for the AFHTTPSessionManager
.
Something like this should work (untested):
NSURL *baseURL = [NSURL URLWithString:@"http://example.com/api"];
NSString *path = @"resource/1";
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
[manager GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject)
{
// Success
NSLog(@"Success: %@", responseObject);
}failure:^(NSURLSessionDataTask *task, NSError *error)
{
// Failure
NSLog(@"Failure: %@", error);
}];
This would send a GET request to http://example.com/api/resource/1
.
来源:https://stackoverflow.com/questions/19561865/how-to-use-afhttpsessionmanager-for-a-get-request