JSON data and SLIM

Good evening,

In my project, I have middleware.php in the same directory as container.php.
container.php:

// construction de l'application $app
    App::class => function (ContainerInterface $container) {
        $app = AppFactory::createFromContainer($container);

        // Configuration de le chemin de la base
        $app->setBasePath('/soignemoi-web'); //https://www.slimframework.com/docs/v4/start/web-servers.html

        $app->addBodyParsingMiddleware(); //Donnees JSON sur serveur WEB

        // Register routes
        (require __DIR__ . '/routes.php')($app);

        //Register middleware
        (require __DIR__ . '/middleware.php')($app);

        return $app;
    },

middleware.php:

<?php

use Slim\App;

return function (App $app) {
    // Parse json, form data and xml
    $app->addBodyParsingMiddleware();

    // Add the Slim built-in routing middleware
    $app->addRoutingMiddleware();

    // Handle exceptions
    $app->addErrorMiddleware(true, true, true);
};

When I use Postman, the JSON data is sent to my database. However, the web page appears in POSTMAN.
I created a mobile application in Java that communicates with my database, but it doesn’t work:

private void transfertJSON() throws IOException {

        URL url = new URL("http://192.168.208.74/soignemoi-web/formulairePrescription");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        // Convertir la HashMap en JSON
        String stringJSON = new Gson().toJson(tableauPrescription);

        // Configurer la connexion
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setDoOutput(true);

        // Écrire les données JSON dans le flux de sortie
        try(OutputStream os = conn.getOutputStream()) {
            byte[] input = stringJSON.getBytes("utf-8");
            os.write(input, 0, input.length);
        }catch (Exception e) {
            Log.e(TAG, "erreur d'écriture: " + e.getMessage(), e);
        }

        // Lire la réponse
        int responseCode = conn.getResponseCode();
        Log.d(TAG, "code de la réponse: " + responseCode);
        if (responseCode == HttpURLConnection.HTTP_OK) {
            Log.d(TAG, "Envoi de données réussi.");
        } else {
            Log.e(TAG, "Envoi de données échoué. Code de la réponse: " + responseCode);
        }
    }

Thank you for your help.

give;

erreur d'écriture: Cleartext HTTP traffic to 192.168.208.74 not permitted
java.io.IOException: Cleartext HTTP traffic to 192.168.208.74 not permitted
at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:127)
etc...

Http is not permitted. I perhaps find a solution.

It works. I had two problems:

  • The HTTP interdiction (not to be used in production)
  • My application wanted to run on the main thread of Android. It’s forbidden.

For the interdiction, I made an XML file named network_security_config.xml in the app/res/xml folder:

<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">192.168.84.74</domain>
    </domain-config>
</network-security-config>

and in app/manifests/AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:networkSecurityConfig="@xml/network_security_config"

For the threads, I use the Retrofit method. It’s long to explain here. ChatGPT4 knows how to do it.

Good afternoon.

It’s strange. With Android Studio, the web page also appears in the Logcat console.