UrlRequest -> JSON -> TableView -> Es werden keine Daten angezeigt und auch kein Error ausgegen

    Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

    • UrlRequest -> JSON -> TableView -> Es werden keine Daten angezeigt und auch kein Error ausgegen

      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?

      Smarty-Template: TableView

      1. mport UIKit
      2. class MenuTableViewController: UITableViewController {
      3. @IBAction func onClickButton(_ sender: Any) {
      4. get_data_from_url(url: "muss noch nicht übergeben werden")
      5. }
      6. var TableData:Array< String > = Array < String >()
      7. override func viewDidLoad() {
      8. super.viewDidLoad()
      9. get_data_from_url(url: "muss noch nicht übergeben werden")
      10. }
      11. // MARK: - Table view data source
      12. override func numberOfSections(in tableView: UITableView) -> Int {
      13. // #warning Incomplete implementation, return the number of sections
      14. return 1
      15. }
      16. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      17. // #warning Incomplete implementation, return the number of rows
      18. return TableData.count
      19. }
      20. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      21. let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
      22. cell.textLabel?.text = TableData[indexPath.row]
      23. return cell
      24. }
      25. func get_data_from_url(url:String)
      26. {
      27. print("Programmstelle 1")
      28. let url = URL(string: "http://127.0.0.1/Menue_Start.php")!
      29. // post the data
      30. var request = URLRequest(url: url)
      31. request.httpMethod = "POST"
      32. let postData = "ID=2&Field2=Field2Data".data(using: .utf8)
      33. request.httpBody = postData
      34. print("Programmstelle 2")
      35. // execute the datatask and validate the result
      36. let task = URLSession.shared.dataTask(with: request) {
      37. (data, response, error) in
      38. //print(data)
      39. print("Programmstelle 3")
      40. if error != nil
      41. {
      42. print("Error")
      43. }
      44. else
      45. {
      46. print(response)
      47. if let content = data
      48. {
      49. do
      50. {
      51. let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
      52. //self.extract_json(jsonData: myJson as! NSData)
      53. if let countries_list = myJson as? NSArray
      54. {
      55. for i in countries_list
      56. //for (var i = 0; i < countries_list.count ; i++ )
      57. {
      58. if let country_obj = countries_list[i as! Int] as? NSDictionary
      59. {
      60. if let country_name = country_obj["menue"] as? String
      61. {
      62. if let country_code = country_obj["id"] as? String
      63. {
      64. self.TableData.append(country_name + " [" + country_code + "]")
      65. }
      66. }
      67. }
      68. }
      69. }
      70. }
      71. catch
      72. {
      73. }
      74. }
      75. }
      76. }
      77. }
      78. }
      Alles anzeigen






      Dass der PHP Aufruf funktioniert sehe ich hier in einem normalen UIViewController mit print JSON, da bekomme ich Daten zurück

      Smarty-Template: UIViewController

      1. class ViewController: UIViewController {
      2. override func viewDidLoad() {
      3. super.viewDidLoad()
      4. let url = URL(string: "http://127.0.0.1/Menue_Start.php")!
      5. // post the data
      6. var request = URLRequest(url: url)
      7. request.httpMethod = "POST"
      8. let postData = "ID=2&Field2=Field2Data".data(using: .utf8)
      9. request.httpBody = postData
      10. // execute the datatask and validate the result
      11. let task = URLSession.shared.dataTask(with: request) {
      12. (data, response, error) in
      13. //print(data)
      14. if error != nil
      15. {
      16. print("Error")
      17. }
      18. else
      19. {
      20. //print(response)
      21. if let content = data
      22. {
      23. do
      24. {
      25. let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
      26. print(myJson)
      27. }
      28. catch
      29. {
      30. }
      31. }
      32. }
      33. }
      34. task.resume()
      35. }
      36. }
      Alles anzeigen
    • Hatte vergessen task.resume() aufzurufen

      Jetzt bekomme ich mit print("Ergebnis" + name) die Werte des Json ausgegeben, jedoch werden sie nicht im TableView angezeigt, hat hier noch wer eine Idee was ich noch machen muss?


      Smarty-Template: TableView

      1. class MenuTableViewController: UITableViewController {
      2. @IBAction func onClickButton(_ sender: Any) {
      3. get_data_from_url(url: "muss noch nicht übergeben werden")
      4. }
      5. var TableData:Array< String > = Array < String >()
      6. override func viewDidLoad() {
      7. super.viewDidLoad()
      8. get_data_from_url(url: "muss noch nicht übergeben werden")
      9. }
      10. // MARK: - Table view data source
      11. override func numberOfSections(in tableView: UITableView) -> Int {
      12. // #warning Incomplete implementation, return the number of sections
      13. return 1
      14. }
      15. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      16. // #warning Incomplete implementation, return the number of rows
      17. return TableData.count
      18. }
      19. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      20. let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
      21. cell.textLabel?.text = TableData[indexPath.row]
      22. return cell
      23. }
      24. func get_data_from_url(url:String)
      25. {
      26. let url = URL(string: "http://127.0.0.1/Menue_Start.php")!
      27. // post the data
      28. var request = URLRequest(url: url)
      29. request.httpMethod = "POST"
      30. let postData = "ID=2&Field2=Field2Data".data(using: .utf8)
      31. request.httpBody = postData
      32. // execute the datatask and validate the result
      33. let task = URLSession.shared.dataTask(with: request) {
      34. (data, response, error) in
      35. //print(data)
      36. if error != nil
      37. {
      38. print("Error")
      39. }
      40. else
      41. {
      42. //print(response)
      43. if let content = data
      44. {
      45. do
      46. {
      47. let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
      48. //self.extract_json(jsonData: myJson as! NSData)
      49. let blogs = myJson["menue"] as? [[String: Any]]
      50. for blog in blogs! {
      51. if let name = blog["ID"] as? String {
      52. self.TableData.append(name)
      53. print("Ergebnis:" + name)
      54. }
      55. if let name = blog["GRUPPE"] as? String {
      56. self.TableData.append(name)
      57. print("Ergebnis:" + name)
      58. }
      59. }
      60. }
      61. catch
      62. {
      63. }
      64. }
      65. }
      66. }
      67. task.resume()
      68. }
      Alles anzeigen