ios - 帖子的神秘网络问题。 Wifi 工作,手机不

标签 ios swift post

当我的实际设备处于 3G 或 4G 网络时,发送帖子请求总是超时。但是,当我使用 wifi 时,服务器端会立即收到发布请求。

这是我的涉及发布请求的代码。我得到一个 Optional("The request timed out.")。我不认为 session 长度应该是一个问题,因为它会在很长一段时间后超时并且正在发送的数据只是一个带密码的用户名。有什么想法吗?

我运行一个 flask 服务器,但我认为它不相关,所以我没有包含它的代码。

       if let jsonData = try? JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted) {
            let url = NSURL(string: base_address + taglocation_address)!
            let request = NSMutableURLRequest(url: url as URL)
            request.httpMethod = "POST"
            request.addValue("application/json", forHTTPHeaderField: "Content-Type")
            request.httpBody = jsonData
            print("starting task")
            let task = URLSession.shared.dataTask(with: request as URLRequest){ data,response,error in
                if error != nil{
                    self.statusLabel.text = error?.localizedDescription
                    print(error?.localizedDescription)
                    return
                }

                do {
                    let json = try JSONSerialization.jsonObject(with: data!, options:  JSONSerialization.ReadingOptions.allowFragments) as? NSDictionary
                    print("doing something")
                    if let responseJSON = json as? [String: String] {
                        print(responseJSON)
                        self.statusLabel.text = responseJSON["status"]

                    }

                } catch let error as NSError {
                    self.statusLabel.text = error.localizedDescription
                    print(error)
                }
            }
            task.resume()
        }

最佳答案

在您的 NSMutableURLRequest 对象中,尝试添加 allowsCellularAccess 属性并将其设置为 true。请参阅下面的代码。

 if let jsonData = try? JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted) {
            let url = NSURL(string: base_address + taglocation_address)!
            let request = NSMutableURLRequest(url: url as URL)
            request.httpMethod = "POST"
            request.addValue("application/json", forHTTPHeaderField: "Content-Type")
            request.httpBody = jsonData
            request.allowsCellularAccess=true //Add this line
            print("starting task")
            let task = URLSession.shared.dataTask(with: request as URLRequest){ data,response,error in
                if error != nil{
                    self.statusLabel.text = error?.localizedDescription
                    print(error?.localizedDescription)
                    return
                }

                do {
                    let json = try JSONSerialization.jsonObject(with: data!, options:  JSONSerialization.ReadingOptions.allowFragments) as? NSDictionary
                    print("doing something")
                    if let responseJSON = json as? [String: String] {
                        print(responseJSON)
                        self.statusLabel.text = responseJSON["status"]

                    }

                } catch let error as NSError {
                    self.statusLabel.text = error.localizedDescription
                    print(error)
                }
            }
            task.resume()
        }

关于ios - 帖子的神秘网络问题。 Wifi 工作,手机不,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43641410/

相关文章:

ios - 为预览图层设置边框会导致边框设置不正确

Ajax 帖子被 firefox 中止(在 Chrome 或 IE 中看不到)

html - HTTP POST 方法是否将数据作为 QueryString 发送?

ios - 从主线程的 Assets 目录加载图像?

ios - CocoaPods Pod 安装奇怪的行为

iOS/swift : Receive callbacks from data streaming service without push notifications while app is in background

ios - 如何使用 Swift 以编程方式添加约束

PHP使用单选按钮的值

ios - Podfile 中 use_frameworks 的问题

ios - 如何确定使用 hyperloop 的 cocoapod 的正确 require 语句是什么?