【问题标题】:Swift POST request and activity indicatorSwift POST 请求和活动指示器
【发布时间】:2018-07-03 19:01:14
【问题描述】:

您好,我正在使用 SwiftyJSON 库从服务器获取数据。我的代码如下:

    let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
    let message2  = "Please wait..."
    var messageMutableString = NSMutableAttributedString()
    messageMutableString = NSMutableAttributedString(string: message2 as String, attributes: [NSFontAttributeName:UIFont(name: "HelveticaNeue-Bold",size: 15.0)!])
    alert.setValue(messageMutableString, forKey: "attributedMessage")
    let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
    loadingIndicator.hidesWhenStopped = true
    loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
    loadingIndicator.startAnimating();



    alert.view.addSubview(loadingIndicator)
    present(alert, animated: true, completion: nil)

    var request = URLRequest(url: URL(string: Global.ipAdres)!)
        request.httpMethod = "POST"
        let postString = Global.postString
        request.httpBody = postString.data(using: .utf8)


    let task = URLSession.shared.dataTask(with: request)
    {                                                   data, response, error in


        DispatchQueue.main.async {
            self.printNameSurname()
            self.dismiss(animated: true, completion: nil)
        }
        guard let data = data, error == nil else {
            print("error=\(String(describing: error))")
            return;
        }
        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200
        {
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(String(describing: response))")
        }            
        let json = JSON(data)

        GlobalVariable.nameSurname = json["data"]["nameSurname"].stringValue + json["data"]["sad"].stringValue
        GlobalVariable.id = json["data"]["id"].stringValue
        GlobalVariable.id2 = json["data"]["id2"].stringValue

    }
    task.resume()

如果我不理解错误,我的 printNameSurname() 函数需要执行并在我的视图控制器中填充两个标签.但是,它们无法正常工作。我的标签没有填充我获取的数据。我认为问题在于发布请求,但我已经搜索了它的示例,但它们对我不起作用或者我做错了什么我该如何解决?

【问题讨论】:

  • self.printNameSurname()GlobalVariable.nameSurname = ... 之前被调用是正常的吗?你应该把DispatchQueue.main.async {self.printNameSurname(); self.dismiss(animated: true, completion: nil)}放在后面吗?
  • 按照您的描述放置我的代码后,它现在可以工作了,谢谢。

标签: ios swift nsurl uiactivityindicatorview swifty-json


【解决方案1】:

得到响应后,将所有代码放入主队列,如下所示。

    let task = URLSession.shared.dataTask(with: request)
    {                                                   data, response, error in

        //Trick here...
        DispatchQueue.main.async {

            self.printNameSurname()
            self.dismiss(animated: true, completion: nil)
            guard let data = data, error == nil else {
                print("error=\(String(describing: error))")
                return;
            }
            if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200
            {
                print("statusCode should be 200, but is \(httpStatus.statusCode)")
                print("response = \(String(describing: response))")
            }
            let json = JSON(data)

            GlobalVariable.nameSurname = json["data"]["nameSurname"].stringValue + json["data"]["sad"].stringValue
            GlobalVariable.id = json["data"]["id"].stringValue
            GlobalVariable.id2 = json["data"]["id2"].stringValue
        }

    }

【讨论】:

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