gpt4 book ai didi

php - Swift Poststring 没有得到我的值

转载 作者:行者123 更新时间:2023-11-28 05:57:34 25 4
gpt4 key购买 nike

我想在我的 poststring 中包含纬度和经度的值以将其发送到我的 php 文件,但它没有给出值 - 我收到此错误代码:

responseString = Optional("")

Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}

我的响应字符串是空的这是我的代码:

import Foundation
import CoreLocation

protocol FeedmodelProtocol: class { func itemsDownloaded(items: NSArray) }

class Feedmodel: NSObject, URLSessionDataDelegate, CLLocationManagerDelegate {

weak var delegate: FeedmodelProtocol!
let locationManager = CLLocationManager() // create Location Manager object
var latitude : Double?
var longitude : Double?

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location: CLLocationCoordinate2D = manager.location?.coordinate else { return }
// set the value of lat and long
latitude = location.latitude
longitude = location.longitude

}

func downloadItems() {
self.locationManager.requestAlwaysAuthorization()

// For use in foreground
// You will need to update your .plist file to request the authorization
self.locationManager.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
locationManager.stopUpdatingLocation()
}
let myUrl = URL(string: "http://example.com/stock_service4.php");
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
var request = URLRequest(url:myUrl!)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let postString = "latiTude=\(String(describing: latitude))&longiTude=\(String(describing: longitude))"
request.httpBody = postString.data(using: .utf8)
let task = defaultSession.dataTask(with: request) { data, response, error in


guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return

}


if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")

}

let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
self.parseJSON(data)

}

task.resume()

}

func parseJSON(_ data:Data) {

var jsonResult = NSArray()

do{
jsonResult = try JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray;
} catch let error as NSError {
print(error)

}

var jsonElement = NSDictionary()
let stocks = NSMutableArray()

for i in 0 ..< jsonResult.count
{
print(jsonResult)
jsonElement = jsonResult[i] as! NSDictionary


let stock = Stockmodel()

//the following insures none of the JsonElement values are nil through optional binding
if let Datum = jsonElement["Datum"] as? String,
let Tankstelle = jsonElement["Tankstelle"] as? String,
let Kraftstoff1 = jsonElement["Kraftstoff1"] as? String,
let Preis1 = jsonElement["Preis1"] as? String,
let Kraftstoff2 = jsonElement["Kraftstoff2"] as? String,
let Preis2 = jsonElement["Preis2"] as? String,
let Notiz = jsonElement["Notiz"] as? String,
let longitude = jsonElement["longitude"] as? String,
let latitude = jsonElement["latitude"] as? String



{
print (Datum)
print(Tankstelle)
print(Kraftstoff1)
print(Preis1)
print(Kraftstoff2)
print(Preis2)
print(Notiz)
print(longitude)
print(latitude)
stock.Datum = Datum
stock.Tankstelle = Tankstelle
stock.Kraftstoff1 = Kraftstoff1
stock.Preis1 = Preis1
stock.Kraftstoff2 = Kraftstoff2
stock.Preis2 = Preis2
stock.Notiz = Notiz
stock.longitude = longitude
stock.latitude = latitude


}

stocks.add(stock)

}

DispatchQueue.main.async(execute: { () -> Void in

self.delegate.itemsDownloaded(items: stocks)

})
}

}

此行不将值发送到我的 php:

  let postString = "latiTude=\(String(describing: latitude))&longiTude=\(String(describing: longitude))"

但是如果我在这一行中手动设置纬度和经度,例如:

let postString = "latiTude=28&longiTude=-99"

它工作完美。

可能是什么错误?

最佳答案

latitudelongitude 变量在 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 中设置,但是这函数在函数 downloadItems() 之后调用(每次位置更新时)。所以你应该像这样在位置管理器中添加 downloadItems() :

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location: CLLocationCoordinate2D = manager.location?.coordinate else { return }
// set the value of lat and long
latitude = location.latitude
longitude = location.longitude

downloadItems()
}

Look at this question

关于php - Swift Poststring 没有得到我的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51011421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com