How to use AFHTTPSessionManager for a GET request?

眉间皱痕 提交于 2019-12-08 07:57:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!