mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-27 15:59:29 +00:00
Connection works better on real devices
This commit is contained in:
parent
7b217acbdb
commit
f7b80e8f6d
@ -14,9 +14,10 @@ android {
|
|||||||
buildTypes {
|
buildTypes {
|
||||||
|
|
||||||
debug {
|
debug {
|
||||||
|
//Connexion to the internal API of Comunic
|
||||||
buildConfigField "String", "api_url", "\"http://devweb.local/comunic/api/\""
|
buildConfigField "String", "api_url", "\"http://devweb.local/comunic/api/\""
|
||||||
buildConfigField "String", "api_service_name", "testService"
|
buildConfigField "String", "api_service_name", "\"testService\""
|
||||||
buildConfigField "String", "api_service_token", "testPasswd"
|
buildConfigField "String", "api_service_token", "\"testPasswd\""
|
||||||
}
|
}
|
||||||
|
|
||||||
release {
|
release {
|
||||||
|
@ -51,7 +51,7 @@ class APIPostData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Return result
|
//Return result
|
||||||
return encoded_key + "&" + encoded_value;
|
return encoded_key + "=" + encoded_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import java.util.ArrayList;
|
|||||||
* Created by pierre on 10/31/17.
|
* Created by pierre on 10/31/17.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class APIParameters {
|
class APIRequestParameters {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parameters of the request
|
* Parameters of the request
|
||||||
@ -25,7 +25,7 @@ class APIParameters {
|
|||||||
*
|
*
|
||||||
* @param uri The request URI on the server
|
* @param uri The request URI on the server
|
||||||
*/
|
*/
|
||||||
APIParameters(String uri){
|
APIRequestParameters(String uri){
|
||||||
//Save request URI
|
//Save request URI
|
||||||
request_uri = uri;
|
request_uri = uri;
|
||||||
|
|
||||||
@ -43,4 +43,34 @@ class APIParameters {
|
|||||||
parameters.add(new APIPostData(name, value));
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
@ -4,12 +4,122 @@ import android.os.AsyncTask;
|
|||||||
|
|
||||||
import org.json.JSONObject;
|
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.
|
* Created by pierre on 10/31/17.
|
||||||
*/
|
*/
|
||||||
|
//TODO Create APIResponse class
|
||||||
|
abstract class APIRequestTask extends AsyncTask<APIRequestParameters, Void, APIResponse> {
|
||||||
|
|
||||||
abstract class APIRequestTask extends AsyncTask<APIParameters, Void, JSONObject> {
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -2,10 +2,15 @@ package org.communiquons.android.comunic.client;
|
|||||||
|
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
|
import android.widget.ProgressBar;
|
||||||
|
import android.widget.ScrollView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
public class LoginActivity extends AppCompatActivity {
|
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_mail = (EditText) findViewById(R.id.email_field);
|
||||||
EditText login_password = (EditText) findViewById(R.id.password_field);
|
EditText login_password = (EditText) findViewById(R.id.password_field);
|
||||||
|
|
||||||
|
|
||||||
int stop = 0;
|
int stop = 0;
|
||||||
|
|
||||||
//Check password
|
//Check password
|
||||||
@ -64,6 +70,37 @@ public class LoginActivity extends AppCompatActivity {
|
|||||||
if(stop != 0)
|
if(stop != 0)
|
||||||
return;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user