# JSON data and SLIM

**URL:** https://discourse.slimframework.com/t/json-data-and-slim/5904
**Category:** Questions
**Created:** [May 20, 2024, 9:10pm UTC](https://discourse.slimframework.com/t/json-data-and-slim/5904 "2024-05-20T21:10:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![samclia](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@samclia](https://discourse.slimframework.com/u/samclia)
#### Post date: [May 20, 2024, 9:10pm UTC](https://discourse.slimframework.com/t/json-data-and-slim/5904/1 "2024-05-20T21:10:54Z")

</div>

Good evening,

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

```auto
// 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:

```auto
<?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:

```auto
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.

---

<div class="post-metadata">

### Author: ![samclia](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@samclia](https://discourse.slimframework.com/u/samclia)
#### Post date: [May 21, 2024, 7:30am UTC](https://discourse.slimframework.com/t/json-data-and-slim/5904/2 "2024-05-21T07:30:08Z")

</div>

> [@samclia](#):
>
> `Log.e(TAG, "erreur d'écriture: " + e.getMessage(), e);`

give;

```auto
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.

---

<div class="post-metadata">

### Author: ![samclia](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@samclia](https://discourse.slimframework.com/u/samclia)
#### Post date: [May 21, 2024, 11:38am UTC](https://discourse.slimframework.com/t/json-data-and-slim/5904/3 "2024-05-21T11:38:32Z")

</div>

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:

```xml
<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
<?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.

---

<div class="post-metadata">

### Author: ![samclia](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@samclia](https://discourse.slimframework.com/u/samclia)
#### Post date: [May 22, 2024, 7:36am UTC](https://discourse.slimframework.com/t/json-data-and-slim/5904/4 "2024-05-22T07:36:23Z")

</div>

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