Hallo zusammen,
ich bin neu in der App Entwicklung und versuche gerade Daten über PHP -> JSON in ein Table View zu laden
Dazu habe ich mir einen TableViewController erstellt und dachte mir wenn man den Button drückt und die Funktion get_data_from_url aufruft sollten die Daten in der Tabelle erscheine
Lasse mir ein paar Dinge ausgeben, um zu sehen bis wohin das Programm springt da ich keine Errormeldung bekomme
print("Programmstelle 2") wird noch ausgegeben
print("Programmstelle 3") erscheint nicht mehr
Hat jemand eine Idee was ich falsch mache?
Alles anzeigen
Dass der PHP Aufruf funktioniert sehe ich hier in einem normalen UIViewController mit print JSON, da bekomme ich Daten zurück
Alles anzeigen
ich bin neu in der App Entwicklung und versuche gerade Daten über PHP -> JSON in ein Table View zu laden
Dazu habe ich mir einen TableViewController erstellt und dachte mir wenn man den Button drückt und die Funktion get_data_from_url aufruft sollten die Daten in der Tabelle erscheine
Lasse mir ein paar Dinge ausgeben, um zu sehen bis wohin das Programm springt da ich keine Errormeldung bekomme
print("Programmstelle 2") wird noch ausgegeben
print("Programmstelle 3") erscheint nicht mehr
Hat jemand eine Idee was ich falsch mache?
Smarty-Template: TableView
- mport UIKit
- class MenuTableViewController: UITableViewController {
- @IBAction func onClickButton(_ sender: Any) {
- get_data_from_url(url: "muss noch nicht übergeben werden")
- }
- var TableData:Array< String > = Array < String >()
- override func viewDidLoad() {
- super.viewDidLoad()
- get_data_from_url(url: "muss noch nicht übergeben werden")
- }
- // MARK: - Table view data source
- override func numberOfSections(in tableView: UITableView) -> Int {
- // #warning Incomplete implementation, return the number of sections
- return 1
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- // #warning Incomplete implementation, return the number of rows
- return TableData.count
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
- cell.textLabel?.text = TableData[indexPath.row]
- return cell
- }
- func get_data_from_url(url:String)
- {
- print("Programmstelle 1")
- let url = URL(string: "http://127.0.0.1/Menue_Start.php")!
- // post the data
- var request = URLRequest(url: url)
- request.httpMethod = "POST"
- let postData = "ID=2&Field2=Field2Data".data(using: .utf8)
- request.httpBody = postData
- print("Programmstelle 2")
- // execute the datatask and validate the result
- let task = URLSession.shared.dataTask(with: request) {
- (data, response, error) in
- //print(data)
- print("Programmstelle 3")
- if error != nil
- {
- print("Error")
- }
- else
- {
- print(response)
- if let content = data
- {
- do
- {
- let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
- //self.extract_json(jsonData: myJson as! NSData)
- if let countries_list = myJson as? NSArray
- {
- for i in countries_list
- //for (var i = 0; i < countries_list.count ; i++ )
- {
- if let country_obj = countries_list[i as! Int] as? NSDictionary
- {
- if let country_name = country_obj["menue"] as? String
- {
- if let country_code = country_obj["id"] as? String
- {
- self.TableData.append(country_name + " [" + country_code + "]")
- }
- }
- }
- }
- }
- }
- catch
- {
- }
- }
- }
- }
- }
- }
Dass der PHP Aufruf funktioniert sehe ich hier in einem normalen UIViewController mit print JSON, da bekomme ich Daten zurück
Smarty-Template: UIViewController
- class ViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- let url = URL(string: "http://127.0.0.1/Menue_Start.php")!
- // post the data
- var request = URLRequest(url: url)
- request.httpMethod = "POST"
- let postData = "ID=2&Field2=Field2Data".data(using: .utf8)
- request.httpBody = postData
- // execute the datatask and validate the result
- let task = URLSession.shared.dataTask(with: request) {
- (data, response, error) in
- //print(data)
- if error != nil
- {
- print("Error")
- }
- else
- {
- //print(response)
- if let content = data
- {
- do
- {
- let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
- print(myJson)
- }
- catch
- {
- }
- }
- }
- }
- task.resume()
- }
- }