美文网首页
Swift 用Alamofire.request 向服务器端传J

Swift 用Alamofire.request 向服务器端传J

作者: 三少爷_贱 | 来源:发表于2017-11-17 16:07 被阅读0次

    var dataOrArray = [[String:AnyObject]]() //定义一个字典数组

    var i = 0

    while (i < stringTest.count) {//stringTest 是我需要传给服务端的值,是一个数组,利用这个循环,把数组的值,都加到字典数组中去

        var dataInfo : [String: AnyObject] = [:]

        dataInfo["commodId"] = stringTest[i].GoodsID as AnyObject?

        dataInfo["commodNum"] = stringTest[i].CommodCount as AnyObject?

        dataOrArray.append(dataInfo)

        i = i + 1

    }

    let url_request = "127.0.0.1"+ "/placeOrder"//请求的地址 + 接口名

    let IsUseIntegral = UserIntegralNum > 0 ? 1 : 2

    let parametes:NSMutableDictionary = NSMutableDictionary()

    parametes["orderType"] = 0

    parametes["remark"] = MessageText.text!

    parametes["consume"] = sum

    parametes["orderIsintegral"] = IsUseIntegral

    parametes["orderInteralamount"] = IntegarlNum

    parametes["couponId"] = CouponInfo.couponId

    parametes["orderAmount"] = sum

    parametes["couponAmount"] = CouponInfo.couponRebate

    parametes["realAmount"] = sum - CouponInfo.couponRebate - IntegarlNum

    parametes["orderFreight"] = orderFreight

    parametes["dealBy"] = 2

    parametes["addrId"] = DefaultAddrinfo.addrId

    parametes["data"] = dataOrArray

    let token = "\(self.tool.getNormalDefult(key: "token")!)"

    let headers = ["Token":token]

    Alamofire.request(url_request, method: HTTPMethod.post, parameters: parametes as! [String:AnyObject], encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in

    if let j = response.result.value{

    //JSON数据解析

    let JSOnDictory = JSON(j)

    print("下单成功:\(JSOnDictory)")

    if (JSOnDictory["status"].intValue == 1) {

    SJProgressHUD.showOnlyText("下单成功!")

    }else{

    SJProgressHUD.showOnlyText("下单失败!")

    }

    //判断返回值

    }

    }

    相关文章

      网友评论

          本文标题:Swift 用Alamofire.request 向服务器端传J

          本文链接:https://www.haomeiwen.com/subject/gcpjvxtx.html