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; ...@@ -3,6 +3,13 @@ package fr.utc.simde.payutc;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; 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.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -39,14 +46,18 @@ public class FoundationListActivity extends BaseActivity { ...@@ -39,14 +46,18 @@ public class FoundationListActivity extends BaseActivity {
dialog.stopLoading(); dialog.stopLoading();
try { try {
if (request.getResponseCode() != 200)
throw new Exception("Malformed JSON");
JsonNode foundationList = request.getJsonResponse();
if (!request.isJsonResponse() || !foundationList.isArray())
throw new Exception("JSON unexpected");
if (request.getResponseCode() == 200 && request.isJsonResponse()) setFoundationList(foundationList);
Log.d(LOG_TAG, "Liste acquise");
// setFoundationList(request.getJsonResponse();
else
dialog.errorDialog(getString(R.string.information_collection), getString(R.string.foundation_error_get_list));
} catch (Exception e) { } catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage()); 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 { ...@@ -63,4 +74,26 @@ public class FoundationListActivity extends BaseActivity {
disconnect(); 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; ...@@ -6,9 +6,10 @@ package fr.utc.simde.payutc.tools;
import android.util.Log; 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.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
...@@ -172,19 +173,30 @@ public class HTTPRequest { ...@@ -172,19 +173,30 @@ public class HTTPRequest {
this.response = builder.toString(); this.response = builder.toString();
} }
public Boolean isJsonResponse() { public Boolean isJsonResponse() throws Exception {
if (request == null) if (request == null)
return 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 { try {
return new JSONObject(response); return new ObjectMapper().readTree(this.response);
} }
catch (Exception e) { 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; ...@@ -3,8 +3,9 @@ package fr.utc.simde.payutc.tools;
import android.app.Activity; import android.app.Activity;
import android.util.Log; import android.util.Log;
import com.fasterxml.jackson.databind.JsonNode;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
...@@ -84,7 +85,7 @@ public class NemopaySession { ...@@ -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( int reponseCode = request(
"KEY", "KEY",
"registerApplication", "registerApplication",
...@@ -95,8 +96,14 @@ public class NemopaySession { ...@@ -95,8 +96,14 @@ public class NemopaySession {
}} }}
); );
if (reponseCode == 200 && this.request.isJsonResponse()) if (reponseCode != 200 || !this.request.isJsonResponse())
this.key = this.request.getJsonResponse().getString("app_key"); 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; return reponseCode;
} }
...@@ -104,11 +111,11 @@ public class NemopaySession { ...@@ -104,11 +111,11 @@ public class NemopaySession {
public int loginApp(final String key, CASConnexion casConnexion) throws Exception { public int loginApp(final String key, CASConnexion casConnexion) throws Exception {
int reponseCode = loginApp(key); int reponseCode = loginApp(key);
JSONObject response = getRequest().getJsonResponse(); JsonNode response = this.request.getJsonResponse();
if (response.has("config") && ((JSONObject) response.get("config")).has("cas_url")) if (response.has("config") && response.get("config").has("cas_url"))
casConnexion.setUrl(((JSONObject) response.get("config")).getString("cas_url")); casConnexion.setUrl(response.get("config").get("cas_url").textValue());
else else
throw new Exception("No correct informations"); throw new Exception("Unexpected JSON");
return reponseCode; return reponseCode;
} }
...@@ -121,19 +128,19 @@ public class NemopaySession { ...@@ -121,19 +128,19 @@ public class NemopaySession {
}} }}
); );
JSONObject response; JsonNode response;
if (reponseCode == 200 && this.request.isJsonResponse()) if (reponseCode == 200 && this.request.isJsonResponse())
response = this.request.getJsonResponse(); response = this.request.getJsonResponse();
else else
throw new Exception("Not authentified"); throw new Exception("Not authentified");
if (response.has("sessionid") && response.has("name")) { if (response.has("sessionid") && response.has("name")) {
this.session = response.getString("sessionid"); this.session = response.get("sessionid").textValue();
this.name = response.getString("name"); this.name = response.get("name").textValue();
this.key = key; this.key = key;
} }
else else
throw new Exception("No correct informations"); throw new Exception("Unexpected JSON");
return reponseCode; return reponseCode;
} }
...@@ -151,18 +158,18 @@ public class NemopaySession { ...@@ -151,18 +158,18 @@ public class NemopaySession {
} }
); );
JSONObject response; JsonNode response;
if (reponseCode == 200 && this.request.isJsonResponse()) if (reponseCode == 200 && this.request.isJsonResponse())
response = this.request.getJsonResponse(); response = this.request.getJsonResponse();
else else
throw new Exception("Not connected"); throw new Exception("Not connected");
if (response.has("sessionid") && response.has("username")) { if (response.has("sessionid") && response.has("username")) {
this.session = response.getString("sessionid"); this.session = response.get("sessionid").textValue();
this.username = response.getString("username"); this.username = response.get("username").textValue();
} }
else else
throw new Exception("No correct informations"); throw new Exception("Unexpected JSON");
return reponseCode; return reponseCode;
} }
...@@ -177,18 +184,18 @@ public class NemopaySession { ...@@ -177,18 +184,18 @@ public class NemopaySession {
}} }}
); );
JSONObject response; JsonNode response;
if (reponseCode == 200 && this.request.isJsonResponse()) if (reponseCode == 200 && this.request.isJsonResponse())
response = this.request.getJsonResponse(); response = this.request.getJsonResponse();
else else
throw new Exception("Not connected"); throw new Exception("Not connected");
if (response.has("sessionid") && response.has("username")) { if (response.has("sessionid") && response.has("username")) {
this.session = response.getString("sessionid"); this.session = response.get("sessionid").textValue();
this.username = response.getString("username"); this.username = response.get("username").textValue();
} }
else else
throw new Exception("No correct informations"); throw new Exception("Unexpected JSON");
return reponseCode; return reponseCode;
} }
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
</LinearLayout> android:orientation="vertical">
\ No newline at end of file
<LinearLayout
android:id="@+id/foundationList"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</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