From 3607cefc237ad4cd104dae4dac244f4bcc1db866 Mon Sep 17 00:00:00 2001 From: Sara <sarasdj@stud.ntnu.no> Date: Wed, 7 Feb 2024 13:52:40 +0100 Subject: [PATCH] update: https connection in fetchData() --- app/lib/consts.dart | 0 app/lib/main.dart | 47 ++++++++++++++++----------------------------- 2 files changed, 17 insertions(+), 30 deletions(-) create mode 100644 app/lib/consts.dart diff --git a/app/lib/consts.dart b/app/lib/consts.dart new file mode 100644 index 00000000..e69de29b diff --git a/app/lib/main.dart b/app/lib/main.dart index d2d9e0c9..042f7ec8 100644 --- a/app/lib/main.dart +++ b/app/lib/main.dart @@ -6,28 +6,12 @@ import 'dart:io'; const String port = "8443"; const String serverURI = "https://127.0.0.1:$port/"; +const String mapEndpoint = "update_map"; +// NB: if http connection fails, run: adb reverse tcp:8443 tcp:8443 const int fetchInterval = 5; -Future<void> main() async { - // NB temporary test print - print(serverURI); - - try { - // Create a custom HTTP client with disabled SSL certificate validation - HttpClient client = HttpClient() - ..badCertificateCallback = - (X509Certificate cert, String host, int port) => true; - - // Use the custom HTTP client for requests - var request = await client.getUrl(Uri.parse(serverURI)); - var response = await request.close(); - - // Handle response - print('Response status: ${response.statusCode}'); - } catch (e) { - // Handle connection error - print('Failed to connect to the server: $e'); - } +void main() { + runApp(App()); } class App extends StatelessWidget { @@ -43,17 +27,20 @@ class App extends StatelessWidget { // fetchData requests data from the update_map endpoint Future<void> fetchData() async { - final response = await http.get(Uri.parse(serverURI)); + // NB: temp test print + print("fetchData triggered!"); + try { + // Custom HTTP client + HttpClient client = HttpClient() + ..badCertificateCallback = // NB: temporary disable SSL certificate validation + (X509Certificate cert, String host, int port) => true; - if (response.statusCode == 200) { - // Parse the JSON response - Map<String, dynamic> data = jsonDecode(response.body); - - // NB temporary test print - print(data); - print("Test print, fetchData triggered"); - } else { // Handle the error case - print('Failed to fetch data. Status code: ${response.statusCode}'); + var request = await client.getUrl(Uri.parse(serverURI+mapEndpoint)); + var response = await request.close(); + + print('Response status: ${response.statusCode}'); + } catch (e) { + print('Failed to connect to the server: $e'); } } -- GitLab