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.