【问题标题】:How to print error html in alamofire in swift?如何在 swift 中在 alamofire 中打印错误 html?
【发布时间】:2018-03-22 05:36:31
【问题描述】:

我正在使用 Alamofire 发出 api 请求。当我的 api 失败时,它会给出 html 响应,因此 alamofire 返回如下错误消息

响应->失败: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "字符 1 周围的值无效。" UserInfo={NSDebugDescription=字符 1 周围的值无效}))

我试过下面的代码

   Alamofire.request(absolutePath(forApi: functionName), method: apiMethod, parameters: parameters, encoding: JSONEncoding.default, headers: defaultHeader())
            .responseJSON { result in

                DILog.print(items: "URL -> \(self.absolutePath(forApi: functionName))")
                if let _ = parameters {
                    DILog.print(items: "Parameters ->\(String(describing: parameters)) ")
                }
                DILog.print(items: "Headers ->\(self.defaultHeader()) ")
                DILog.print(items: "Reponse  ->\(result) ")
                DILog.print(items: "Reponse1  ->\(result.value) ")
                DILog.print(items: "Reponse Code ->\(result.response?.statusCode) ")

                if let errorResponse = result.error {
                    print(errorResponse.localizedDescription)
                    failure(self.parseError(error: errorResponse))
                }
}

我想将错误响应打印为 API 返回的 html。请告诉我该怎么做。

【问题讨论】:

  • 试试“debugPrint(result)”

标签: ios swift alamofire


【解决方案1】:

将响应数据转换为字符串,如下所示

if let _ = result.error {
  print("--------- Error -------")
  if let responseData = result.data {
    let htmlString = String(data: responseData, encoding: .utf8)
    print(htmlString!)
  }
}

【讨论】:

    【解决方案2】:

    希望打印 Alamofire 的回复对您有所帮助 https://github.com/netguru/ResponseDetective

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多