Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
optimization: when responseSerializer is set a AFHTTPResponseSerializ…
Browse files Browse the repository at this point in the history
…er instance
  • Loading branch information
leleliu008 committed Jun 8, 2018
1 parent 2fba9eb commit ddb7550
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion AFNetworkActivityLogger/AFNetworkActivityConsoleLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,24 @@ - (void)URLSessionTaskDidFinish:(NSURLSessionTask *)task withResponseObject:(id)
break;
}
} else {
id responseBody = responseObject;

This comment has been minimized.

Copy link
@leleliu008

leleliu008 Jun 8, 2018

when responseSerializer is set a AFHTTPResponseSerializer instance, the responseObject is NSData Type, in fact, the response body may be xml、html、x-www-form-urlencoded or other text format, in this case , we can according to Content-Type response header determine whether the response body is text, if the response body is text, we convert it to NSString to print it, beasuse it looks better.


if(responseHeaderFields != nil && [responseObject isKindOfClass:[NSData class]]) {
id contentTypeObj = [responseHeaderFields objectForKey:@"Content-Type"];
if([contentTypeObj isKindOfClass:[NSString class]]) {
NSString *contentType = contentTypeObj;
if([contentType containsString:@"application/json"]
|| [contentType containsString:@"application/xml"]
|| [contentType containsString:@"application/x-www-form-urlencoded"]
|| [contentType containsString:@"text/html"]) {
responseBody = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
}
}
}

switch (self.level) {
case AFLoggerLevelDebug:
NSLog(@"%ld '%@' [%.04f s]: %@ %@", (long)responseStatusCode, [[task.response URL] absoluteString], elapsedTime, responseHeaderFields, responseObject);
NSLog(@"%ld '%@' [%.04f s]: %@ %@", (long)responseStatusCode, [[task.response URL] absoluteString], elapsedTime, responseHeaderFields, responseBody);
break;
case AFLoggerLevelInfo:
NSLog(@"%ld '%@' [%.04f s]", (long)responseStatusCode, [[task.response URL] absoluteString], elapsedTime);
Expand Down

0 comments on commit ddb7550

Please sign in to comment.