Browse Source

httpOkClientTest

master
art.dambrine 5 years ago
parent
commit
17f6db7b96
  1. 1
      app/build.gradle
  2. 32
      app/src/main/java/com/speculos/myapplicationoc/TagDetectedActivityAdmin.java

1
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'

32
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);
// 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(response);
maListeJson = new JsonTraductor(responseHttp.body().string());
String violationMessage = ((maListeJson.getJsonData("violations", 0)));
Toast.makeText(TagDetectedActivityAdmin.this, "Message: "+violationMessage, Toast.LENGTH_LONG).show();
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();

Loading…
Cancel
Save