zoukankan      html  css  js  c++  java
  • AFNetworking 网络错误提示data转换字符串

    AFN在进行网络交互时,有时候会碰到返回502、500、404的时候。后台的总需要你配合他查出问题所在。但是AFN在返回数据序列化时解析错误只会转成NSData类型的数据,如果直接扔给后台Data的数据显然有点不靠谱。所以可以在AFURLResponseSerialization.m中加上对data转成string的方法。方法如下:

    在AFURLResponseSerialization.m中找到 “AFNetworkingOperationFailingURLResponseDataErrorKey”在其下方加上

    #ifdef DEBUG

    NSString * const AFNetworkingOperationFailingURLResponseStringErrorKey =  @"com.alamofire.serialization.response.error.string";

    #endif

    ps:主要是在debug状态下需要调试,所以加上环境的判断防止错误。

    第一:

    然后分别在- (BOOL)validateResponse:(NSHTTPURLResponse )response data:(NSData )data error:(NSError * __autoreleasing *)error 方法中在判断data的代码中有两处需要替换:

    if (data) {

    mutableUserInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] = data;

    }

    替换成、

    if (data) {

    mutableUserInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] = data;

    #ifdef DEBUG

    mutableUserInfo[AFNetworkingOperationFailingURLResponseStringErrorKey] = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    #endif

    }

    之后当你碰到网络错误时。AFN会把错误输出,可以搜索console中搜索com.alamofire.serialization.response.error.string:对应的就是data转成的字符串。

    第二:

    NSError *underError = error.userInfo[@"NSUnderlyingError"];

    NSData *data=underError.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];

    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"%@", str);

  • 相关阅读:
    游戏 黑白棋
    题解 P2472 【[SCOI2007]蜥蜴】
    题解 P1682 【过家家】
    题解 P3153 【[CQOI2009]跳舞】
    题解 P2763 【试题库问题】
    题解 P1345 【[USACO5.4]奶牛的电信Telecowmunication】
    网络流----最大流
    Tarjan缩点
    C#之抽象类
    C#之深复制学习案例
  • 原文地址:https://www.cnblogs.com/HMJ-29/p/6066631.html
Copyright © 2011-2022 走看看