Swift2 我们可以在swift中获得HttpStatus代码错误描述吗?
我正在我的应用程序中使用Uber Api。当我试图预订一辆车时,出现了一个问题。我得到了一个httpStatus 400错误。根据他们的文件,这可能是由于各种因素造成的。 我如何通过状态代码(始终为“400”)知道给定类型的哪个描述导致它 我现在的代码:Swift2 我们可以在swift中获得HttpStatus代码错误描述吗?,swift2,swift2.3,Swift2,Swift2.3,我正在我的应用程序中使用Uber Api。当我试图预订一辆车时,出现了一个问题。我得到了一个httpStatus 400错误。根据他们的文件,这可能是由于各种因素造成的。 我如何通过状态代码(始终为“400”)知道给定类型的哪个描述导致它 我现在的代码: if let httpstatuscode = response as? NSHTTPURLResponse { if (error != nil) {
if let httpstatuscode = response as? NSHTTPURLResponse
{
if (error != nil)
{
print("Error : \(error?.localizedDescription)")
}
else if httpstatuscode.statusCode != 202 && httpstatuscode.statusCode != 409
{
print("Status Code\(httpstatuscode.statusCode)")
dispatch_async(dispatch_get_main_queue(), { () -> Void in
CommonViewController.alertViewUI("Alert", message: "Internal Server Error!")
})
}
else if httpstatuscode.statusCode == 409
{
// self.getUberRideLive()
print("Status Code\(httpstatuscode.statusCode)")
CommonViewController.alertViewUI("Alert", message: "You are already on a ride!")
}
您可以使用
httpurresponse
如果您想获得HTTP错误的描述,这是有效的;如果您想从UBER错误中获得描述,您应该查看UBER操作的JSON响应
然后,您的代码应该看起来像
let message: String = HTTPURLResponse.localizedString(forStatusCode: httpstatuscode.statusCode)
一旦获得错误代码
400
,您就可以检查错误的原因。使用消除技术来消除可能性(你在帖子中提到过)。一个接一个地检查,如果你不能关注这个错误,你可以有一个默认的处理方法。示例:检查电子邮件是否已确认等。我没有使用Uber API的经验,但根据,错误响应有一个JSON正文,其中包含机器和人类可读的错误消息。实际上有许多状态代码。每个状态代码包含多个描述。我怎么能检查它?