后台传输的数据中一旦带有\r、\n等特殊字符时AFNetWorking会出现无法解析的情况,那么怎么办呢,我们就可以采用修改af框架的做法来对特殊字符进行转码过滤。
- (id)responseObjectForResponse:(NSURLResponse*)response
data:(NSData*)data
error:(NSError*__autoreleasing*)error
{
if(![selfvalidateResponse:(NSHTTPURLResponse*)responsedata:dataerror:error]) {
if (!error || AFErrorOrUnderlyingErrorHasCodeInDomain(*error, NSURLErrorCannotDecodeContentData, AFURLResponseSerializationErrorDomain)) {
returnnil;
}
}
// Workaround for behavior of Rails to return a single space for `head :ok` (a workaround for a bug in Safari), which is not interpreted as valid input by NSJSONSerialization.
// See https://github.com/rails/rails/issues/1742
NSStringEncodingstringEncoding =self.stringEncoding;
if(response.textEncodingName) {
CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)response.textEncodingName);
if (encoding != kCFStringEncodingInvalidId) {
stringEncoding =CFStringConvertEncodingToNSStringEncoding(encoding);
}
}
idresponseObject =nil;
NSError*serializationError =nil;
@autoreleasepool {
NSString*responseString = [[NSStringalloc]initWithData:dataencoding:stringEncoding];
if(responseString && ![responseStringisEqualToString:@" "]) {
// Workaround for a bug in NSJSONSerialization when Unicode character escape codes are used instead of the actual character
// See http://stackoverflow.com/a/12843465/157142
data = [responseStringdataUsingEncoding:NSUTF8StringEncoding];
if(data) {
if([datalength] >0) {
responseObject = [NSJSONSerializationJSONObjectWithData:dataoptions:self.readingOptionserror:&serializationError];
if(responseObject ==nil) {
//将字符串的特殊字符都屏蔽
NSCharacterSet *controlChars = [NSCharacterSet controlCharacterSet];
NSRangerange = [responseStringrangeOfCharacterFromSet:controlChars];
if(range.location!=NSNotFound)
{
NSMutableString*mutable = [NSMutableStringstringWithString:responseString];
while(range.location!=NSNotFound)
{
[mutabledeleteCharactersInRange:range];
range = [mutablerangeOfCharacterFromSet:controlChars];
}
responseString = mutable;
}
data = [responseStringdataUsingEncoding:NSUTF8StringEncoding];
responseObject = [NSJSONSerializationJSONObjectWithData:dataoptions:self.readingOptionserror:&serializationError];
if(responseObject !=nil) {
error =nil;
}
//将字符串的特殊字符都屏蔽
#ifdef DEBUG
// dispatch_async( dispatch_get_main_queue(), ^{
// [Utility showTitle:responseString];//updataby lv
// });
#endif
}
}else{
returnnil;
}
}else{
NSDictionary*userInfo =@{
NSLocalizedDescriptionKey: NSLocalizedStringFromTable(@"Data failed decoding as a UTF-8 string", @"AFNetworking", nil),
NSLocalizedFailureReasonErrorKey: [NSStringstringWithFormat:NSLocalizedStringFromTable(@"Could not decode string: %@",@"AFNetworking",nil), responseString]
};
serializationError = [NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:userInfo];
}
}
}
if(self.removesKeysWithNullValues&& responseObject) {
responseObject =AFJSONObjectByRemovingKeysWithNullValues(responseObject,self.readingOptions);
}
if(error) {
*error =AFErrorWithUnderlyingError(serializationError, *error);
}
returnresponseObject;
}
网友评论