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);

  • 相关阅读:
    天梯赛5-12 愿天下有情人都是失散多年的兄妹 【dfs】
    poj2718 Smallest Difference【贪心】
    HDU problem 5635 LCP Array【思维】
    codeforces 782C Andryusha and Colored Balloons【构造】
    HDU 4278 Faulty Odometer【进制转换】
    codeforces B. The Meeting Place Cannot Be Changed【二分】
    POJ 3264 Balanced Lineup 【线段树】
    HDU 1850
    CodeForces-714C
    HDU Problem 1247 Hat's Words 【字典树】
  • 原文地址:https://www.cnblogs.com/HMJ-29/p/6066631.html
Copyright © 2011-2022 走看看