From 60fbf745b4ddc6d184ee9d269ee7031829717f44 Mon Sep 17 00:00:00 2001 From: JulietteSCHILLING <juliette.schilling2@etu.univ-lorraine.fr> Date: Mon, 31 Oct 2022 10:49:58 +0100 Subject: [PATCH] enregistrement json ok --- .idea/gradle.xml | 1 + .../com/example/myhabitat/MainActivity.java | 94 ++++++++++++++++++- 2 files changed, 90 insertions(+), 5 deletions(-) diff --git a/.idea/gradle.xml b/.idea/gradle.xml index df527ea..33b144a 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -1,5 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> + <component name="GradleMigrationSettings" migrationVersion="1" /> <component name="GradleSettings"> <option name="linkedExternalProjectsSettings"> <GradleProjectSettings> diff --git a/app/src/main/java/com/example/myhabitat/MainActivity.java b/app/src/main/java/com/example/myhabitat/MainActivity.java index 8e9bbe5..694ec28 100644 --- a/app/src/main/java/com/example/myhabitat/MainActivity.java +++ b/app/src/main/java/com/example/myhabitat/MainActivity.java @@ -2,6 +2,7 @@ package com.example.myhabitat; import android.content.Intent; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.util.Log; import android.view.View; import android.widget.TextView; @@ -12,10 +13,13 @@ import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import habitat.Habitat; +import habitat.Mur; +import habitat.Piece; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; +import java.io.*; public class MainActivity extends AppCompatActivity { @@ -65,7 +69,87 @@ public class MainActivity extends AppCompatActivity { } } - public Habitat getHabitat(){ - return this.habitat; + public void enregistrement(){ + JSONObject enregistrement = new JSONObject(); + JSONArray pieces = new JSONArray(); + JSONArray murs = new JSONArray(); + + for(Piece piece : habitat.getPieces()){ + for(Mur mur : piece.getMurs()){ + JSONObject Jmur = new JSONObject(); + try { + Jmur.put("Orientation", mur.getOrientation()); + Jmur.put("Id", mur.getId()); + } catch (JSONException e) { + throw new RuntimeException(e); + } + murs.put(Jmur); + } + pieces.put(piece.getNom()); + pieces.put(murs); + } + try { + enregistrement.put("Pieces", pieces); + } catch (JSONException e) { + throw new RuntimeException(e); + } + + if(enregistrement != null){ + FileOutputStream fos = null; + try { + fos = openFileOutput("enregistrement.json", MODE_PRIVATE); + PrintStream ps = new PrintStream(fos); + ps.print(enregistrement); + ps.close(); + fos.flush(); + Log.i("testEnregistrement", "enregistrement.json a bien été enregistré"); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + Log.i("testJSON", enregistrement.toString()); + + FileInputStream fis = null; + try { + fis = openFileInput("enregistrement.json"); + } catch (FileNotFoundException e) { + //throw new RuntimeException(e); + } + if (fis != null) { + String json = getFileContent(fis); + + Log.i("testJSON", json); + } + + + }else{ + Log.i("testJSON", "pbm"); + } + } + + public String getFileContent( FileInputStream fis ) { + StringBuilder sb = new StringBuilder(); + Reader r = null; //or whatever encoding + try { + r = new InputStreamReader(fis, "UTF-8"); + int ch = r.read(); + while(ch >= 0) { + sb.append((char)ch); + ch = r.read(); + } + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + return sb.toString(); + } + + @Override + protected void onPause() { + Log.i("testOn", "on y est !"); + enregistrement(); + super.onPause(); } } \ No newline at end of file -- GitLab