Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用Almofirwe框架进行RESTAPI调用?_Ios_Alamofire - Fatal编程技术网

Ios 如何使用Almofirwe框架进行RESTAPI调用?

Ios 如何使用Almofirwe框架进行RESTAPI调用?,ios,alamofire,Ios,Alamofire,我使用下面的代码使用Alamofireframework调用restapi func getHomeList() { let userId = UserDefaults.standard.string(forKey: "user_id")! let parameters: Parameters = [ "my_id" : "\(userId)", "flage" : "1" ] UIApplication.sh

我使用下面的代码使用
Alamofire
framework调用restapi

func getHomeList()
{        
    let userId = UserDefaults.standard.string(forKey: "user_id")!

    let parameters: Parameters = [
        "my_id" : "\(userId)",
        "flage" : "1"
    ]


    UIApplication.shared.isNetworkActivityIndicatorVisible = true

    let headers: HTTPHeaders = [
        "Content-Type": "application/json",
        "Accept": "application/json"
    ]


    Alamofire.request(URL(string: "\(Constants.webApi)\(Constants.gethistory)")!, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON(completionHandler: { (response:DataResponse<Any>) in

        UIApplication.shared.isNetworkActivityIndicatorVisible = false

        self.arrList = NSMutableArray()

        if let followers = response.result.value as? NSDictionary
        {
            print(followers)

            if let status: Int = followers.object(forKey: "status") as? Int
            {

                if status == 1
                {

                    if let arrData: NSMutableArray = followers.object(forKey: "data") as? NSMutableArray
                    {

                        self.arrList  = arrData

                    }

                }
                else
                {

                    let message = MessageView.viewFromNib(layout: .MessageViewIOS8)
                    message.configureTheme(.info)
                    message.configureDropShadow()
                    message.configureContent(title: "Sorry", body: "Something went wrong. Please Try Again")

                    var messageConfig = SwiftMessages.Config()
                    messageConfig.presentationStyle = .bottom
                    messageConfig.presentationContext = .window(windowLevel: UIWindowLevelNormal)
                    messageConfig.duration = .automatic

                    SwiftMessages.show(config: messageConfig, view: message)

                }

                self.tblList.reloadData()

            }

        }
    })

    self.refreshControl.endRefreshing()
}
func getHomeList()
{        
让userId=UserDefaults.standard.string(forKey:“user\u id”)!
let参数:参数=[
“我的用户id”:“\(用户id)”,
“鞭毛”:“1”
]
UIApplication.shared.isNetworkActivityIndicatorVisible=true
let头:HTTPHeaders=[
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”
]
Alamofire.request(URL(字符串:\(Constants.webApi)\(Constants.gethistory))!,方法:.post,参数:参数,编码:JSONEncoding.default,头:头)。响应JSON(completionHandler:{(响应:DataResponse)在
UIApplication.shared.isNetworkActivityIndicatorVisible=false
self.arrList=NSMutableArray()
如果让followers=response.result.value为?NSDictionary
{
印刷品(追随者)
如果让status:Int=followers.object(forKey:status)作为?Int
{
如果状态==1
{
如果让arrData:NSMutableArray=followers.object(forKey:“data”)作为?NSMutableArray
{
self.arrList=arrData
}
}
其他的
{
让message=MessageView.viewFromNib(布局:.MessageViewIOS8)
message.configurateme(.info)
message.configureDropShadow()
message.configureContent(标题:“抱歉”,正文:“出现问题,请重试”)
var messageConfig=SwiftMessages.Config()
messageConfig.presentationStyle=.bottom
messageConfig.presentationContext=.window(windowLevel:UIWindowLevelNormal)
messageConfig.duration=.automatic
show(配置:messageConfig,视图:message)
}
self.tblList.reloadData()
}
}
})
self.refreshControl.endRefreshing()
}
但我不知道如何在api中没有得到响应,以及如何检查网络是否可访问


另外,如果请求未成功完成,如何处理?

您可以使用下面的代码片段检查请求成功与否

Alamofire.request("your URL", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

    switch(response.result) {
    case .success(_):
        if let data = response.result.value{
            print(response.result.value)
        }
        break

    case .failure(_):
        print(response.result.error)
        break

    }
}
Alamofire.request(“您的URL”,方法:.post,参数:parameters,编码:JSONEncoding.default,头:nil).responseJSON{(响应:DataResponse)in
开关(response.result){
成功案例:
如果let data=response.result.value{
打印(响应.结果.值)
}
打破
案例.失败(uu):
打印(响应.结果.错误)
打破
}
}