From 17f6db7b96d77d5cf8ee74c78a1c141f448757fd Mon Sep 17 00:00:00 2001 From: "art.dambrine" Date: Sun, 12 Apr 2020 19:32:16 +0200 Subject: [PATCH] httpOkClientTest --- app/build.gradle | 1 + .../TagDetectedActivityAdmin.java | 40 ++++++++++++++----- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c95c00b..6475882 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -22,6 +22,7 @@ android { dependencies { implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + implementation 'com.squareup.okhttp3:okhttp:4.5.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' diff --git a/app/src/main/java/com/speculos/myapplicationoc/TagDetectedActivityAdmin.java b/app/src/main/java/com/speculos/myapplicationoc/TagDetectedActivityAdmin.java index 804cc55..e813244 100644 --- a/app/src/main/java/com/speculos/myapplicationoc/TagDetectedActivityAdmin.java +++ b/app/src/main/java/com/speculos/myapplicationoc/TagDetectedActivityAdmin.java @@ -2,7 +2,6 @@ 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; @@ -14,6 +13,12 @@ import androidx.annotation.Nullable; import java.io.IOException; import java.util.regex.Pattern; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + public class TagDetectedActivityAdmin extends Activity { String hexa_id, mail, username; @@ -98,16 +103,29 @@ public class TagDetectedActivityAdmin extends Activity { // 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+"}"); - Toast.makeText(TagDetectedActivityAdmin.this, "Message: " + response, Toast.LENGTH_LONG).show(); - Log.d("DEBUG-CLIENT",response); - - try{ - maListeJson = new JsonTraductor(response); - String violationMessage = ((maListeJson.getJsonData("violations", 0))); - Toast.makeText(TagDetectedActivityAdmin.this, "Message: "+violationMessage, Toast.LENGTH_LONG).show(); - - } catch (Exception e) {} +// response = APICommandes.postMethod(srvURL + "/api/bracelets","{\"nfcId\": \"" + hexa_id + "\", \"client\": \"/api/clients/" + idClient + "\", \"state\" :"+0+"}"); + + OkHttpClient client = new OkHttpClient().newBuilder() + .build(); + MediaType mediaType = MediaType.parse("application/json"); + RequestBody body = RequestBody.create(mediaType, "{\"nfcId\": \""+hexa_id+"\",\"client\": \"/api/clients/"+idClient+"\",\"state\": 0}"); + Request request = new Request.Builder() + .url(srvURL + "/api/bracelets") + .method("POST", body) + .addHeader("Content-Type", "application/json") + .build(); + Response 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) {} + } } catch (IOException e) { e.printStackTrace();