后臺傳輸?shù)臄?shù)據(jù)中一旦帶有\(zhòng)r、\n等特殊字符時AFNetWorking會出現(xiàn)無法解析的情況,那么怎么辦呢,我們就可以采用修改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;
}