10 changed files with 266 additions and 365 deletions
Binary file not shown.
Binary file not shown.
@ -1,150 +0,0 @@ |
|||
package com.speculos.myapplicationoc; |
|||
|
|||
import android.os.StrictMode; |
|||
import android.util.Log; |
|||
|
|||
import java.io.BufferedReader; |
|||
import java.io.DataOutputStream; |
|||
import java.io.IOException; |
|||
import java.io.InputStreamReader; |
|||
import java.net.HttpURLConnection; |
|||
import java.net.URL; |
|||
import java.nio.charset.StandardCharsets; |
|||
|
|||
public class APICommandes { |
|||
|
|||
private static HttpURLConnection con; |
|||
private static String response; |
|||
private static int SDK_INT = android.os.Build.VERSION.SDK_INT; |
|||
|
|||
|
|||
/* |
|||
A propos de l'implementation JWT : |
|||
|
|||
Using the token |
|||
HttpURLConnection con = (HttpURLConnection) url.openConnection(); |
|||
con.setRequestProperty("Authorization", "Bearer " + accessToken); |
|||
*/ |
|||
|
|||
|
|||
static String getMethod(String url) throws IOException { |
|||
|
|||
|
|||
if (SDK_INT > 8) /*Eviter l'erreur android qui empêche call API dans le main thread*/ |
|||
{ |
|||
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() |
|||
.permitAll().build(); |
|||
StrictMode.setThreadPolicy(policy); |
|||
//your codes here
|
|||
} |
|||
|
|||
try { |
|||
|
|||
URL myurl = new URL(url); |
|||
Log.d("API-debug","GET sur myurl: "+url); |
|||
|
|||
con = (HttpURLConnection) myurl.openConnection(); |
|||
con.setConnectTimeout(5000); |
|||
con.setReadTimeout(5000); |
|||
|
|||
|
|||
con.setRequestMethod("GET"); |
|||
|
|||
StringBuilder content; |
|||
|
|||
try (BufferedReader in = new BufferedReader( |
|||
new InputStreamReader(con.getInputStream()))) { |
|||
|
|||
String line; |
|||
content = new StringBuilder(); |
|||
|
|||
while ((line = in.readLine()) != null) { |
|||
|
|||
content.append(line); |
|||
content.append(System.lineSeparator()); |
|||
} |
|||
} |
|||
|
|||
response = content.toString(); |
|||
|
|||
|
|||
|
|||
} catch (Exception e){ |
|||
// handle exception here
|
|||
Log.d("API-debug","Exception, GET a echoue, we return empty string"); |
|||
return ""; |
|||
|
|||
} finally { |
|||
|
|||
con.disconnect(); |
|||
} |
|||
|
|||
return response; |
|||
} |
|||
|
|||
|
|||
public static String postMethod(String url, String urlParameters) throws IOException { |
|||
/* |
|||
Exemple de paramètres pour faire un POST : |
|||
|
|||
String urlParameters = "{\"name\": \"user4\", \"solde\": 22, \"email\": \"user4@mail.fr\"}"; |
|||
*/ |
|||
|
|||
if (SDK_INT > 8) /*Eviter l'erreur android qui empêche call API dans le main thread*/ |
|||
{ |
|||
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() |
|||
.permitAll().build(); |
|||
StrictMode.setThreadPolicy(policy); |
|||
//your codes here
|
|||
} |
|||
|
|||
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); |
|||
|
|||
try { |
|||
|
|||
URL myurl = new URL(url); |
|||
con = (HttpURLConnection) myurl.openConnection(); |
|||
con.setConnectTimeout(5000); |
|||
con.setReadTimeout(5000); |
|||
|
|||
con.setDoOutput(true); |
|||
con.setRequestMethod("POST"); |
|||
con.setRequestProperty("User-Agent", "Java client"); |
|||
con.setRequestProperty("Content-Type", "application/json"); |
|||
|
|||
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) { |
|||
|
|||
wr.write(postData); |
|||
} |
|||
|
|||
StringBuilder content; |
|||
|
|||
try (BufferedReader br = new BufferedReader( |
|||
new InputStreamReader(con.getInputStream()))) { |
|||
|
|||
String line; |
|||
content = new StringBuilder(); |
|||
|
|||
while ((line = br.readLine()) != null) { |
|||
content.append(line); |
|||
content.append(System.lineSeparator()); |
|||
} |
|||
} |
|||
|
|||
//System.out.println(content.toString());
|
|||
response = content.toString(); |
|||
|
|||
} catch (Exception e) { |
|||
|
|||
Log.d("API-debug","Exception, le call POST echoue"); |
|||
|
|||
} finally { |
|||
|
|||
con.disconnect(); |
|||
} |
|||
|
|||
return response; |
|||
} |
|||
|
|||
} |
|||
|
@ -1,92 +0,0 @@ |
|||
package com.speculos.myapplicationoc; |
|||
|
|||
import android.util.Log; |
|||
|
|||
import com.google.gson.JsonArray; |
|||
import com.google.gson.JsonElement; |
|||
import com.google.gson.JsonObject; |
|||
import com.google.gson.JsonParser; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
|
|||
public class JsonTraductor { |
|||
|
|||
private String json; |
|||
|
|||
private List<JsonObject> listJson = new ArrayList<JsonObject>(); |
|||
|
|||
|
|||
|
|||
public JsonTraductor(){ |
|||
// Json de test
|
|||
// {"id":1,"nfcId":"azerty","client":{"id":1,"name":"Omer"},"state":1}
|
|||
// "[{\"id\":1,\"nfcId\":\"azerty\",\"client\":{\"id\":1,\"name\":\"Omer\"},\"state\":1},{\"id\":2,\"nfcId\":\"oui\",\"client\":null,\"state\":0}]";
|
|||
json = "{\"id\":1,\"nfcId\":\"azerty\",\"client\":{\"id\":1,\"name\":\"Omer\"},\"state\":1}"; |
|||
} |
|||
|
|||
JsonTraductor(String jsonEntry){ |
|||
json = jsonEntry; |
|||
buildList(); |
|||
} |
|||
|
|||
private void buildList(){ |
|||
// Parse le json et tiens à jour une liste d'objets json
|
|||
listJson.clear(); |
|||
|
|||
try { |
|||
|
|||
if(jsonIsArray(json)){ |
|||
|
|||
JsonArray entries = (JsonArray) new JsonParser().parse(json); |
|||
//String id = ((JsonObject)entries.get(0)).get("id").toString();
|
|||
//System.out.println(id);
|
|||
|
|||
for(int i = 0; i<entries.size(); i++){ |
|||
listJson.add(((JsonObject)entries.get(i))); |
|||
} |
|||
|
|||
|
|||
} else { |
|||
|
|||
JsonParser parser = new JsonParser(); |
|||
JsonElement jsonElement = parser.parse(json); |
|||
JsonObject jsonObjectOne = jsonElement.getAsJsonObject(); |
|||
//System.out.println(jsonObject.get("id"));
|
|||
listJson.add(jsonObjectOne); |
|||
//System.out.println(listJson.get(0).get("id"));
|
|||
} |
|||
|
|||
} catch (Exception e){ |
|||
// handle exception here
|
|||
} |
|||
|
|||
|
|||
} |
|||
|
|||
private static Boolean jsonIsArray(String json){ |
|||
Log.d("JsonTraductor-debug","Json: "+json); |
|||
|
|||
if (json != null){ |
|||
return json.toCharArray()[0] != '{'; |
|||
} else { |
|||
return false; |
|||
} |
|||
|
|||
|
|||
} |
|||
|
|||
|
|||
String getJsonData(String jsonId, int jsonIndex){ |
|||
|
|||
if(jsonIndex >= listJson.size()){ |
|||
// pour ne pas depasser l'index final
|
|||
return null; |
|||
|
|||
} else { |
|||
return listJson.get(jsonIndex).get(jsonId).toString(); |
|||
} |
|||
|
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue