zoukankan      html  css  js  c++  java
  • Alamofire的get请求,post请求,以及上传字典

    get请求:

    Alamofire.request("你的网址",method: .get, parameters: nil, encoding: URLEncoding.default)

                .responseJSON { response in

              //print("url:(response.request)")  // original URL request

              //print("request:(response.response)") // URL response

              //print("data:(response.data)")     // server data

              //print("result:(response.result)")   // result of response serializatio

            }

    post请求:

     let currRequest = Alamofire.request("你的网址", method: .post, parameters: nil, encoding:URLEncoding.default, headers: nil)

                currRequest.responseJSON{(responseJson) -> Void in

                    

                    switch responseJson.result {

                    case .success(let value):

                        let swiftyJsonVar = JSON(value)

                        //将获取的所有的title放到数组里面

    //                    let str = swiftyJsonVar["data"].arrayValue.map({$0["title"].stringValue})

                        self.resultArr = swiftyJsonVar["data"]

    //                    for index in 1...self.resultArr.count{

    //                        if let str = self.resultArr[index-1]["title"].string {

    //

    //                             print(str)

    //                        }

    //                    }

                    case .failure( _):

    //                    print(error)

                     

                         ZHBPopTipView.showText("网络错误", position: .bottom)

                    }

                    self.tableView.reloadData()

                }

     


    post接口并上传字典:

      do {

                 dic = ["key":"value"]

                 print(mobileDic)

                Alamofire.request("你的网址", method: .post, parameters:dic, encoding: URLEncoding.default, headers:nil).responseJSON(completionHandler: { (response)in

                    switch response.result{

                    case .success:

                        iflet dic:NSDictionary = (response.result.value as? NSDictionary){

                            print(dic)

                        }

                    case .failure(let error):

                        print(error)

                    }

                })

            } catch  {

                print(error)

            }

  • 相关阅读:
    C# 视频监控系列(11):H264播放器——封装API[HikPlayM4.dll]
    php框架
    ExtJS带验证码登录框[新增回车提交]
    ant 读取环境变量的值
    Apache Velocity实现模板化
    23种设计模式概述
    android资源下载
    无序hashset与hashmap让其有序
    PermGen space错误解决方法
    设计模式之代理模式(Proxy)
  • 原文地址:https://www.cnblogs.com/-ios/p/7279367.html
Copyright © 2011-2022 走看看