Commit 9b39e556 authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

FoundationList view

Create view to print all foundations
Work now with Jackson (no more JSONObject objects)
Cleaning
Fix bugs
parent 7a755e74
......@@ -3,6 +3,13 @@ package fr.utc.simde.payutc;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.json.JSONArray;
import org.json.JSONObject;
......@@ -39,14 +46,18 @@ public class FoundationListActivity extends BaseActivity {
dialog.stopLoading();
try {
if (request.getResponseCode() != 200)
throw new Exception("Malformed JSON");
if (request.getResponseCode() == 200 && request.isJsonResponse())
Log.d(LOG_TAG, "Liste acquise");
// setFoundationList(request.getJsonResponse();
else
dialog.errorDialog(getString(R.string.information_collection), getString(R.string.foundation_error_get_list));
JsonNode foundationList = request.getJsonResponse();
if (!request.isJsonResponse() || !foundationList.isArray())
throw new Exception("JSON unexpected");
setFoundationList(foundationList);
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
dialog.errorDialog(getString(R.string.information_collection), getString(R.string.foundation_error_get_list));
}
}
});
......@@ -63,4 +74,26 @@ public class FoundationListActivity extends BaseActivity {
disconnect();
}
protected void setFoundationList(JsonNode foundationList) throws Exception {
LinearLayout linearLayout = findViewById(R.id.foundationList);
for (final JsonNode foundation : foundationList) {
Button foundationButton = new Button(this);
if (!foundation.has("name") || !foundation.has("fun_id"))
throw new Exception("Unexpected JSON");
foundationButton.setText(foundation.get("name").textValue());
final String idFoundation = foundation.get("fun_id").textValue();
foundationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Log.d(LOG_TAG, idFoundation);
}
});
linearLayout.addView(foundationButton);
}
}
}
......@@ -6,9 +6,10 @@ package fr.utc.simde.payutc.tools;
import android.util.Log;
import org.json.JSONArray;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
......@@ -172,19 +173,30 @@ public class HTTPRequest {
this.response = builder.toString();
}
public Boolean isJsonResponse() {
public Boolean isJsonResponse() throws Exception {
if (request == null)
return null;
return request.getContentType() == "application/json";
if (request.getContentType().equals("application/json")) {
try {
new ObjectMapper().readTree(this.response);
return true;
} catch (IOException e) {
throw new Exception("Malformed JSON");
}
}
else {
return false;
}
}
public Object getJsonResponse() throws IOException, JSONException {
public JsonNode getJsonResponse() throws IOException, JSONException {
try {
return new JSONObject(response);
return new ObjectMapper().readTree(this.response);
}
catch (Exception e) {
return new JSONArray(response);
Log.e(LOG_TAG, "error: " + e.getMessage());
return null;
}
}
......
......@@ -3,8 +3,9 @@ package fr.utc.simde.payutc.tools;
import android.app.Activity;
import android.util.Log;
import com.fasterxml.jackson.databind.JsonNode;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.HashMap;
......@@ -84,7 +85,7 @@ public class NemopaySession {
);
}
public int registerApp(final String name, final String description, final String service) throws IOException, JSONException {
public int registerApp(final String name, final String description, final String service) throws Exception {
int reponseCode = request(
"KEY",
"registerApplication",
......@@ -95,8 +96,14 @@ public class NemopaySession {
}}
);
if (reponseCode == 200 && this.request.isJsonResponse())
this.key = this.request.getJsonResponse().getString("app_key");
if (reponseCode != 200 || !this.request.isJsonResponse())
throw new Exception("Not created");
JsonNode response = this.request.getJsonResponse();
if (response.has("app_key"))
this.key = response.get("app_key").textValue();
else
throw new Exception("Unexpected JSON");
return reponseCode;
}
......@@ -104,11 +111,11 @@ public class NemopaySession {
public int loginApp(final String key, CASConnexion casConnexion) throws Exception {
int reponseCode = loginApp(key);
JSONObject response = getRequest().getJsonResponse();
if (response.has("config") && ((JSONObject) response.get("config")).has("cas_url"))
casConnexion.setUrl(((JSONObject) response.get("config")).getString("cas_url"));
JsonNode response = this.request.getJsonResponse();
if (response.has("config") && response.get("config").has("cas_url"))
casConnexion.setUrl(response.get("config").get("cas_url").textValue());
else
throw new Exception("No correct informations");
throw new Exception("Unexpected JSON");
return reponseCode;
}
......@@ -121,19 +128,19 @@ public class NemopaySession {
}}
);
JSONObject response;
JsonNode response;
if (reponseCode == 200 && this.request.isJsonResponse())
response = this.request.getJsonResponse();
else
throw new Exception("Not authentified");
if (response.has("sessionid") && response.has("name")) {
this.session = response.getString("sessionid");
this.name = response.getString("name");
this.session = response.get("sessionid").textValue();
this.name = response.get("name").textValue();
this.key = key;
}
else
throw new Exception("No correct informations");
throw new Exception("Unexpected JSON");
return reponseCode;
}
......@@ -151,18 +158,18 @@ public class NemopaySession {
}
);
JSONObject response;
JsonNode response;
if (reponseCode == 200 && this.request.isJsonResponse())
response = this.request.getJsonResponse();
else
throw new Exception("Not connected");
if (response.has("sessionid") && response.has("username")) {
this.session = response.getString("sessionid");
this.username = response.getString("username");
this.session = response.get("sessionid").textValue();
this.username = response.get("username").textValue();
}
else
throw new Exception("No correct informations");
throw new Exception("Unexpected JSON");
return reponseCode;
}
......@@ -177,18 +184,18 @@ public class NemopaySession {
}}
);
JSONObject response;
JsonNode response;
if (reponseCode == 200 && this.request.isJsonResponse())
response = this.request.getJsonResponse();
else
throw new Exception("Not connected");
if (response.has("sessionid") && response.has("username")) {
this.session = response.getString("sessionid");
this.username = response.getString("username");
this.session = response.get("sessionid").textValue();
this.username = response.get("username").textValue();
}
else
throw new Exception("No correct informations");
throw new Exception("Unexpected JSON");
return reponseCode;
}
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/foundationList"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
\ No newline at end of file
</LinearLayout>
</ScrollView>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment