Ios 关于帖子的神秘网络问题。Wifi工作,手机不工作';T
当我的实际设备在3G或4G上时,我发送post请求时总是会超时。但是,当我使用wifi时,服务器端会立即收到post请求 这是我的代码,涉及post请求。我得到一个Ios 关于帖子的神秘网络问题。Wifi工作,手机不工作';T,ios,swift,post,Ios,Swift,Post,当我的实际设备在3G或4G上时,我发送post请求时总是会超时。但是,当我使用wifi时,服务器端会立即收到post请求 这是我的代码,涉及post请求。我得到一个可选(“请求超时”)。我不认为会话长度应该是一个问题,因为它在很长一段时间后超时,并且发送的数据只是一个带有密码的用户名。有什么想法吗 我运行了一个flask服务器,但我认为它不相关,所以我没有包括它的代码 if let jsonData = try? JSONSerialization.data(withJSONObj
可选(“请求超时”)
。我不认为会话长度应该是一个问题,因为它在很长一段时间后超时,并且发送的数据只是一个带有密码的用户名。有什么想法吗
我运行了一个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
对象中,尝试添加allowscellaraccess
属性并将其设置为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()
}
当您确实请求时会发生什么。AllowCellularAccess=true它会正常工作…谢谢!你能把这个答案贴出来让我给你打分吗?是的,当然。我将发布一个答案。出于某种原因,它只是停止工作(因为我们部署了)。我添加了请求。allowscellaraccess=true。还有其他原因吗?不幸的是,“为什么它不能工作”有太多可能的组合,无法放入评论中。您必须在调试方面具有创造性。您可以ping您试图访问的web服务吗?您提到您的网络服务在wifi上是一致的,是否说在Verizon或AT&T等其他手机提供商网络上也是一致的?