AFNetworking Get full http response

You should listen for the AFNetworkingOperationDidStartNotification and AFNetworkingOperationDidFinishNotification events that fire at the beginning and end of the operation:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(YourOperationStartMethod:) 
                                             name:AFNetworkingOperationDidStartNotification 
                                           object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(YourOperationFinishMethod:) 
                                             name:AFNetworkingOperationDidFinishNotification 
                                           object:nil];

You can grab the operation object from the notification like this:

AFHTTPRequestOperation *operation = 
        (AFHTTPRequestOperation *) [notification object];

This operation object contains request and response properties.

A good example of how all this works can be found in the AFHTTPRequestOperationLogger plugin (which, of course, you could simply use, instead of writing your own thing).


转载:http://stackoverflow.com/questions/18385810/afnetworking-get-full-http-response



AFNetworkActivityLogger

AFNetworkActivityLogger is an extension for AFNetworking 2.0 that logs network requests as they are sent and received.

AFNetworkActivityLogger listens for AFNetworkingOperationDidStartNotification / AFNetworkingOperationDidFinishNotification and AFNetworkingTaskDidStartNotification / AFNetworkingTaskDidFinishNotificationnotifications, which are posted by AFNetworking as request operations and session tasks are started and finish. For further customization of logging output, users are encouraged to implement desired functionality by listening for these notifications.

Usage

Add the following code to AppDelegate.m -application:didFinishLaunchingWithOptions::

[[AFNetworkActivityLogger sharedLogger] startLogging];

Now all AFURLConnectionOperation and NSURLSessionTask objects created by an AFURLSessionManager will have their request and response logged to the console, a la:

GET http://example.com/foo/bar.json
200 http://example.com/foo/bar.json

If the default logging level is too verbose—say, if you only want to know when requests fail—then changing it is as simple as:

[[AFNetworkActivityLogger sharedLogger] setLevel:AFLoggerLevelError];

参考:https://github.com/AFNetworking/AFNetworkActivityLogger

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值