iOS中遇到后臺返回數(shù)據(jù)中帶有\(zhòng)r、\n等特殊字符時

后臺傳輸?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;

}

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容