@ -2,6 +2,7 @@ package com.speculos.myapplicationoc;
import android.app.Activity ;
import android.os.Bundle ;
import android.util.Log ;
import android.view.View ;
import android.widget.Button ;
import android.widget.EditText ;
@ -10,6 +11,9 @@ import android.widget.Toast;
import androidx.annotation.Nullable ;
import org.json.JSONException ;
import org.json.JSONObject ;
import java.io.IOException ;
import java.util.regex.Pattern ;
@ -21,12 +25,10 @@ import okhttp3.Response;
public class TagDetectedActivityAdmin extends Activity {
String hexa_id , mail , username ;
String hexa_id , mail , username , srvURL ;
// recuperation de MyApplication contenant les variables globales
// MyApplication myApplication = (MyApplication) getApplication();
String srvURL ;
// Temporaire
// String nfcId = "azerty";
@ -82,28 +84,51 @@ public class TagDetectedActivityAdmin extends Activity {
Toast . makeText ( TagDetectedActivityAdmin . this , "Création de compte NFC Cashless, mail:" + mail + " name:" + username + " bracelet:" + hexa_id , Toast . LENGTH_LONG ) . show ( ) ;
String response = "" ;
Response responseHttp = null ;
try {
response = APICommandes . postMethod ( srvURL + "/api/clients" , "{\"name\": \"" + username + "\", \"email\": \"" + mail + "\", \"solde\" :" + 0 + "}" ) ;
// response = APICommandes.postMethod(srvURL + "/api/clients","{\"name\": \"" + username + "\", \"email\": \"" + mail + "\", \"solde\" :"+0+"}");
OkHttpClient client = new OkHttpClient ( ) . newBuilder ( )
. build ( ) ;
MediaType mediaType = MediaType . parse ( "application/json" ) ;
RequestBody body = RequestBody . create ( mediaType , "{\"name\": \"" + username + "\",\"email\": \"" + mail + "\",\"solde\": 0}" ) ;
Request request = new Request . Builder ( )
. url ( srvURL + "/api/clients" )
. method ( "POST" , body )
. addHeader ( "Content-Type" , "application/json" )
. build ( ) ;
responseHttp = client . newCall ( request ) . execute ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
Toast . makeText ( TagDetectedActivityAdmin . this , "Error" , Toast . LENGTH_LONG ) . show ( ) ;
}
if ( response . length ( ) < = 3 ) {
if ( responseHttp . body ( ) . toString ( ) . length ( ) < = 3 ) {
// Info sur le retour du POST Creation client
Toast . makeText ( TagDetectedActivityAdmin . this , "Server not responding: " + response , Toast . LENGTH_LONG ) . show ( ) ;
Toast . makeText ( TagDetectedActivityAdmin . this , "Server not responding: " + responseHttp . body ( ) . toString ( ) , Toast . LENGTH_LONG ) . show ( ) ;
} else {
// Creation du compte réussie, link le bracelet au compte client (TODO: penser à faire les verifs si bracelet existe avant de créer un compte client)
// Une fois qu'on a vérifié que le bracelet n'existe pas dans la base on l'ajoute au client
/ * Creation du compte réussie , link le bracelet au compte client ( TODO : penser à faire les verifs si bracelet existe avant de créer un compte client )
Une fois qu ' on a vérifié que le bracelet n ' existe pas dans la base on l ' ajoute au client
On commence par parser la reponse de l ' appel POST précédent pour avoir l ' idClient sans refaire de requête * /
JSONObject jsonReader = null ;
int idClient = 0 ;
try {
jsonReader = new JSONObject ( responseHttp . body ( ) . string ( ) ) ;
idClient = jsonReader . getInt ( "id" ) ;
} catch ( JSONException e ) {
e . printStackTrace ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
// On commence par parser la reponse de l'appel POST précédent pour avoir l'idClient sans refaire de requête
JsonTraductor maListeJson = new JsonTraductor ( response ) ;
int idClient = Integer . parseInt ( ( maListeJson . getJsonData ( "id" , 0 ) ) ) ;
// On peut maintenant link le bracelet grace à l'hexa_id et l'idClient
try {
// response = APICommandes.postMethod(srvURL + "/api/bracelets","{\"nfcId\": \"" + hexa_id + "\", \"client\": \"/api/clients/" + idClient + "\", \"state\" :"+0+"}");
OkHttpClient client = new OkHttpClient ( ) . newBuilder ( )
. build ( ) ;
@ -114,17 +139,16 @@ public class TagDetectedActivityAdmin extends Activity {
. method ( "POST" , body )
. addHeader ( "Content-Type" , "application/json" )
. build ( ) ;
Response responseHttp = client . newCall ( request ) . execute ( ) ;
responseHttp = client . newCall ( request ) . execute ( ) ;
if ( responseHttp . code ( ) = = 400 ) {
try {
maListeJson = new JsonTraductor ( responseHttp . body ( ) . string ( ) ) ;
String violationMessage = ( ( maListeJson . getJsonData ( "violations" , 0 ) ) ) ;
maListeJson = new JsonTraductor ( violationMessage ) ;
violationMessage = ( ( maListeJson . getJsonData ( "message" , 0 ) ) ) ;
Toast . makeText ( TagDetectedActivityAdmin . this , "ID NFC: " + violationMessage , Toast . LENGTH_LONG ) . show ( ) ;
} catch ( Exception e ) { }
jsonReader = new JSONObject ( responseHttp . body ( ) . string ( ) ) ;
Log . d ( "JSON-MSG" , "violations: " + jsonReader . getJSONArray ( "violations" ) . getString ( 0 ) ) ;
Toast . makeText ( TagDetectedActivityAdmin . this , "violations: " + jsonReader . getJSONArray ( "violations" ) . getString ( 0 ) , Toast . LENGTH_LONG ) . show ( ) ;
} catch ( JSONException e ) {
e . printStackTrace ( ) ;
}
}
} catch ( IOException e ) {