Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set HTTP request using AFNetworking 2?

I need to send ordinary HTTP request (GET) and answer will in text/html. How can I send this response using AFNetworkin 2 ?

Now I'm trying to use

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://example.com"]];
[self HTTPRequestOperationWithRequest:request
                              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                  NSLog(@"JSON: %@", responseObject);
                              } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                  NSLog(@"Error: %@", error);
                              }];

And was frustrates - it do nothing. When debugging, nor success nor fail clause have been triggered.

Also I tried to use GET:parameters:success:failure: method, but in response I see this error:

Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html"

Please, anybody can explain me what are wrong and what is the correct way to send request (if I will get response as text/html)?

Regards, Alex.

like image 998
LIAL Avatar asked Dec 02 '22 17:12

LIAL


2 Answers

You said in your comment, in response to the suggestion to use the AFHTTPRequestOperationManager:

When I used GET I got this error as I wrote above: Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html"

You can remedy that with a AFHTTPResponseSerializer:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:@"https://example.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // do whatever you'd like here; for example, if you want to convert 
    // it to a string and log it, you might do something like:

    NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"%@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

You can also use AFHTTPRequestOperation:

NSOperationQueue *networkQueue = [[NSOperationQueue alloc] init];
networkQueue.maxConcurrentOperationCount = 5;

NSURL *url = [NSURL URLWithString:@"https://example.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    // do whatever you'd like here; for example, if you want to convert 
    // it to a string and log it, you might do something like:

    NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"%@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%s: AFHTTPRequestOperation error: %@", __FUNCTION__, error);
}];
[networkQueue addOperation:operation];

Ideally, though, it's advisable to write server code that returns JSON (or XML), as that's much easier for an app to consume and parse.

like image 190
Rob Avatar answered Dec 14 '22 20:12

Rob


//AFN 2.0 is just support IOS 7,and it's standard use as follow:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" 
  parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSLog(@"JSON: %@", responseObject)
     }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"Error: %@", error);
     }
];
like image 40
kevn liu Avatar answered Dec 14 '22 18:12

kevn liu