From f7b80e8f6ddb52fdf2878e88179a1ffe1cbc51f3 Mon Sep 17 00:00:00 2001 From: Pierre Date: Wed, 1 Nov 2017 08:07:01 +0100 Subject: [PATCH] Connection works better on real devices --- app/build.gradle | 5 +- .../android/comunic/client/APIPostData.java | 2 +- ...ameters.java => APIRequestParameters.java} | 34 +++++- .../comunic/client/APIRequestTask.java | 112 +++++++++++++++++- .../android/comunic/client/APIResponse.java | 83 +++++++++++++ .../android/comunic/client/LoginActivity.java | 37 ++++++ 6 files changed, 267 insertions(+), 6 deletions(-) rename app/src/main/java/org/communiquons/android/comunic/client/{APIParameters.java => APIRequestParameters.java} (55%) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/APIResponse.java diff --git a/app/build.gradle b/app/build.gradle index 2a144f5..6d722dc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -14,9 +14,10 @@ android { buildTypes { debug { + //Connexion to the internal API of Comunic buildConfigField "String", "api_url", "\"http://devweb.local/comunic/api/\"" - buildConfigField "String", "api_service_name", "testService" - buildConfigField "String", "api_service_token", "testPasswd" + buildConfigField "String", "api_service_name", "\"testService\"" + buildConfigField "String", "api_service_token", "\"testPasswd\"" } release { diff --git a/app/src/main/java/org/communiquons/android/comunic/client/APIPostData.java b/app/src/main/java/org/communiquons/android/comunic/client/APIPostData.java index 27eb401..237bb39 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/APIPostData.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/APIPostData.java @@ -51,7 +51,7 @@ class APIPostData { } //Return result - return encoded_key + "&" + encoded_value; + return encoded_key + "=" + encoded_value; } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/APIParameters.java b/app/src/main/java/org/communiquons/android/comunic/client/APIRequestParameters.java similarity index 55% rename from app/src/main/java/org/communiquons/android/comunic/client/APIParameters.java rename to app/src/main/java/org/communiquons/android/comunic/client/APIRequestParameters.java index b4a59cd..08a37bf 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/APIParameters.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/APIRequestParameters.java @@ -8,7 +8,7 @@ import java.util.ArrayList; * Created by pierre on 10/31/17. */ -class APIParameters { +class APIRequestParameters { /** * Parameters of the request @@ -25,7 +25,7 @@ class APIParameters { * * @param uri The request URI on the server */ - APIParameters(String uri){ + APIRequestParameters(String uri){ //Save request URI request_uri = uri; @@ -43,4 +43,34 @@ class APIParameters { parameters.add(new APIPostData(name, value)); } + /** + * Retrieve request URI + * + * @return The request URI + */ + String getRequest_uri() { + return request_uri; + } + + /** + * Return all the request parameters as a string ready to be passed to the request output + * stream. + * + * @return A string + */ + String get_parameters_encoded(){ + + //Return string + String result = ""; + + //Process loop + for(int i = 0; i < parameters.size(); i++){ + + //Make sure to separate parameters + result += (i > 0 ? "&" : ""); + result += parameters.get(i).get_encoded(); + } + + return result; + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/APIRequestTask.java b/app/src/main/java/org/communiquons/android/comunic/client/APIRequestTask.java index 4fcddb0..c2cbf54 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/APIRequestTask.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/APIRequestTask.java @@ -4,12 +4,122 @@ import android.os.AsyncTask; import org.json.JSONObject; +import java.io.BufferedOutputStream; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.net.HttpURLConnection; +import java.net.URL; + /** + * Perform an API request on the server + * * Created by pierre on 10/31/17. */ +//TODO Create APIResponse class +abstract class APIRequestTask extends AsyncTask { -abstract class APIRequestTask extends AsyncTask { + /** + * Background task + * + * Warning: This method support only one request per object + * + * @param params Parametres required to perform the API request + * @return JSONObject The result of the request + */ + @Override + protected APIResponse doInBackground(APIRequestParameters... params) { + + try { + //Perform the API request + return downloadUrl(params[0]); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + + + } + + /** + * The next action to do once we got a response is implemented by the method that perform + * the request. + * + * @param result The result of the request + */ + abstract protected void onPostExecute(APIResponse result); + + /** + * Peform an API request on the server and return the result as a JSON code + * + * @param parameters The parameters to pass to the server + * @return The result of the request + */ + private APIResponse downloadUrl(APIRequestParameters parameters) throws Exception{ + + APIResponse result = new APIResponse(); + + InputStream is = null; + + try { + + //Determine the URL of the request + URL url = new URL(BuildConfig.api_url + parameters.getRequest_uri()); + + //The request is being performed on an http server + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + + //Configure the connection + conn.setReadTimeout(3000); + conn.setConnectTimeout(3000); + conn.setRequestMethod("POST"); + conn.setDoInput(true); + conn.setDoOutput(true); + //Send request parameters + OutputStream out = conn.getOutputStream(); + OutputStreamWriter outputStreamWriter = new OutputStreamWriter(out, "UTF-8"); + BufferedWriter writer = new BufferedWriter(outputStreamWriter); + String parameters_encoded = parameters.get_parameters_encoded(); + writer.write(parameters_encoded); + writer.flush(); + writer.close(); + out.close(); + + //Connect to the server + conn.connect(); + + + //Get response code + result.setResponse_code(conn.getResponseCode()); + + is = conn.getInputStream(); + String response = readIt(is, 5000); + result.setResponse(response); + + conn.disconnect(); + + } finally { + //Close streams + if(is != null) + is.close(); + } + //Return result + return result; + } + + // Reads an InputStream and converts it to a String. + private String readIt(InputStream stream, int len) throws IOException { + Reader reader = null; + reader = new InputStreamReader(stream, "UTF-8"); + char[] buffer = new char[len]; + reader.read(buffer); + return new String(buffer); + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/APIResponse.java b/app/src/main/java/org/communiquons/android/comunic/client/APIResponse.java new file mode 100644 index 0000000..b2d65c3 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/APIResponse.java @@ -0,0 +1,83 @@ +package org.communiquons.android.comunic.client; + +import org.json.JSONException; +import org.json.JSONObject; + +/** + * Store and serve the response of an API request + * + * Created by pierre on 10/31/17. + */ + +class APIResponse { + + /** + * Reponse code + */ + private int response_code; + + /** + * Response string + */ + private String response = null; + + /** + * Constructor of the API response + */ + APIResponse(){} + + /** + * Set response string + * + * @param response The request response + */ + void setResponse(String response) { + this.response = response; + } + + /** + * Set the response code + * + * @param response_code The response code + */ + void setResponse_code(int response_code) { + this.response_code = response_code; + } + + /** + * Get the response code + * + * @return The response code of the request + */ + public int getResponse_code() { + return response_code; + } + + /** + * Get the response as a string + * @return The response + */ + public String getResponseString() { + return response; + } + + /** + * Get the response as a JSON object + * + * @return The response as JSON object. False in case of failure + */ + public JSONObject getJSONObject(){ + + JSONObject response = null; + + //Try to decode JSON object + try { + response = new JSONObject(this.response); + } catch (JSONException e) { + //In case of failure + e.printStackTrace(); + } + + return response; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java index d7c82be..5a274e8 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java @@ -2,10 +2,15 @@ package org.communiquons.android.comunic.client; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.util.Log; import android.view.View; import android.widget.EditText; +import android.widget.ProgressBar; +import android.widget.ScrollView; import android.widget.Toast; +import org.json.JSONObject; + public class LoginActivity extends AppCompatActivity { /** @@ -44,6 +49,7 @@ public class LoginActivity extends AppCompatActivity { EditText login_mail = (EditText) findViewById(R.id.email_field); EditText login_password = (EditText) findViewById(R.id.password_field); + int stop = 0; //Check password @@ -64,6 +70,37 @@ public class LoginActivity extends AppCompatActivity { if(stop != 0) return; + open_loading_state(true); + //Perform a request on the API to check user credentials and get login tokens + APIRequestParameters params = new APIRequestParameters("user/connectUSER"); + params.addParameter("userMail", ""+login_mail.getText()); + params.addParameter("userPassword", ""+login_password.getText()); + + //Create Request + new APIRequestTask(){ + @Override + protected void onPostExecute(APIResponse result) { + + open_loading_state(false); + + + + } + }.execute(params); + } + + /** + * Switch between loading state and ready state for the login form + * + * @param show_progress Specify wether a progress bar should be shown or not + */ + void open_loading_state(boolean show_progress){ + //Grab elements + ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar); + ScrollView login_form = (ScrollView) findViewById(R.id.login_form); + + progressBar.setVisibility(show_progress ? View.VISIBLE : View.GONE); + login_form.setVisibility(show_progress ? View.GONE : View.VISIBLE); } }