mirror of
				https://github.com/pierre42100/ComunicAndroid
				synced 2025-11-04 11:34:06 +00:00 
			
		
		
		
	Ready to create ApIRequest task file
This commit is contained in:
		@@ -12,6 +12,13 @@ android {
 | 
			
		||||
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
 | 
			
		||||
    }
 | 
			
		||||
    buildTypes {
 | 
			
		||||
 | 
			
		||||
        debug {
 | 
			
		||||
            buildConfigField "String", "api_url", "\"http://devweb.local/comunic/api/\""
 | 
			
		||||
            buildConfigField "String", "api_service_name", "testService"
 | 
			
		||||
            buildConfigField "String", "api_service_token", "testPasswd"
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        release {
 | 
			
		||||
            minifyEnabled false
 | 
			
		||||
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,12 @@
 | 
			
		||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 | 
			
		||||
    package="org.communiquons.android.comunic.client">
 | 
			
		||||
 | 
			
		||||
    <!-- Internet access is required to access the API -->
 | 
			
		||||
    <uses-permission android:name="android.permission.INTERNET" />
 | 
			
		||||
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    <application
 | 
			
		||||
        android:allowBackup="true"
 | 
			
		||||
        android:fullBackupContent="@xml/backup_scheme"
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,46 @@
 | 
			
		||||
package org.communiquons.android.comunic.client;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This class handles the setup of the parametres required to setup an API request
 | 
			
		||||
 *
 | 
			
		||||
 * Created by pierre on 10/31/17.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
class APIParameters {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Parameters of the request
 | 
			
		||||
     */
 | 
			
		||||
    private ArrayList<APIPostData> parameters;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The URI on the server
 | 
			
		||||
     */
 | 
			
		||||
    private String request_uri;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The class constructor
 | 
			
		||||
     *
 | 
			
		||||
     * @param uri The request URI on the server
 | 
			
		||||
     */
 | 
			
		||||
    APIParameters(String uri){
 | 
			
		||||
        //Save request URI
 | 
			
		||||
        request_uri = uri;
 | 
			
		||||
 | 
			
		||||
        //Intializate parametres array
 | 
			
		||||
        parameters = new ArrayList<>();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Add a new parameter to the request
 | 
			
		||||
     *
 | 
			
		||||
     * @param name The name of the new key
 | 
			
		||||
     * @param value The value of the new key
 | 
			
		||||
     */
 | 
			
		||||
    void addParameter(String name, String value){
 | 
			
		||||
        parameters.add(new APIPostData(name, value));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,57 @@
 | 
			
		||||
package org.communiquons.android.comunic.client;
 | 
			
		||||
 | 
			
		||||
import java.net.URLEncoder;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This class stores on parameter for a POST request
 | 
			
		||||
 *
 | 
			
		||||
 * Created by pierre on 10/31/17.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
class APIPostData {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The name of the key
 | 
			
		||||
     */
 | 
			
		||||
    private String key_name;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The value of the key
 | 
			
		||||
     */
 | 
			
		||||
    private String key_value;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Public constructor of the class
 | 
			
		||||
     *
 | 
			
		||||
     * @param name The name of the parameter
 | 
			
		||||
     * @param value The value of the parameter
 | 
			
		||||
     */
 | 
			
		||||
    APIPostData(String name, String value){
 | 
			
		||||
        key_name = name;
 | 
			
		||||
        key_value = value;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the the name and the value of the key in an encoded form
 | 
			
		||||
     *
 | 
			
		||||
     * @return The result
 | 
			
		||||
     */
 | 
			
		||||
    String get_encoded(){
 | 
			
		||||
 | 
			
		||||
        String encoded_key;
 | 
			
		||||
        String encoded_value;
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            encoded_key = URLEncoder.encode(key_name, "UTF-8");
 | 
			
		||||
            encoded_value = URLEncoder.encode(key_value, "UTF-8");
 | 
			
		||||
        } catch (java.io.UnsupportedEncodingException e){
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
 | 
			
		||||
            return "";
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //Return result
 | 
			
		||||
        return encoded_key + "&" + encoded_value;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,15 @@
 | 
			
		||||
package org.communiquons.android.comunic.client;
 | 
			
		||||
 | 
			
		||||
import android.os.AsyncTask;
 | 
			
		||||
 | 
			
		||||
import org.json.JSONObject;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Created by pierre on 10/31/17.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
abstract class APIRequestTask extends AsyncTask<APIParameters, Void, JSONObject> {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -2,12 +2,68 @@ package org.communiquons.android.comunic.client;
 | 
			
		||||
 | 
			
		||||
import android.support.v7.app.AppCompatActivity;
 | 
			
		||||
import android.os.Bundle;
 | 
			
		||||
import android.view.View;
 | 
			
		||||
import android.widget.EditText;
 | 
			
		||||
import android.widget.Toast;
 | 
			
		||||
 | 
			
		||||
public class LoginActivity extends AppCompatActivity {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Utilities object
 | 
			
		||||
     */
 | 
			
		||||
    private Utilities utils;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void onCreate(Bundle savedInstanceState) {
 | 
			
		||||
        super.onCreate(savedInstanceState);
 | 
			
		||||
        setContentView(R.layout.activity_login);
 | 
			
		||||
 | 
			
		||||
        //Create utilities object
 | 
			
		||||
        utils = new Utilities(this);
 | 
			
		||||
 | 
			
		||||
        //Make login submit button lives
 | 
			
		||||
        findViewById(R.id.login_submit).setOnClickListener(new View.OnClickListener() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onClick(View v) {
 | 
			
		||||
 | 
			
		||||
                //Submit login form
 | 
			
		||||
                submitLogin();
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Handle login form submission
 | 
			
		||||
     */
 | 
			
		||||
    void submitLogin(){
 | 
			
		||||
 | 
			
		||||
        //Get the fields
 | 
			
		||||
        EditText login_mail = (EditText) findViewById(R.id.email_field);
 | 
			
		||||
        EditText login_password = (EditText) findViewById(R.id.password_field);
 | 
			
		||||
 | 
			
		||||
        int stop = 0;
 | 
			
		||||
 | 
			
		||||
        //Check password
 | 
			
		||||
        if(login_password.length()<3){
 | 
			
		||||
            login_password.setError(getString(R.string.activity_login_err_invalid_password));
 | 
			
		||||
            login_password.requestFocus();
 | 
			
		||||
            stop = 1;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //Check email address
 | 
			
		||||
        if(login_mail.length() < 3 || !utils.isValidMail(login_mail.getText())){
 | 
			
		||||
            login_mail.setError(getString(R.string.activity_login_err_invalid_email));
 | 
			
		||||
            login_mail.requestFocus();
 | 
			
		||||
            stop = 1;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //Stop if required
 | 
			
		||||
        if(stop != 0)
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,9 @@
 | 
			
		||||
package org.communiquons.android.comunic.client;
 | 
			
		||||
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
import android.text.TextUtils;
 | 
			
		||||
import android.util.Log;
 | 
			
		||||
import android.util.Patterns;
 | 
			
		||||
 | 
			
		||||
import java.io.FileInputStream;
 | 
			
		||||
import java.io.FileOutputStream;
 | 
			
		||||
@@ -91,4 +93,14 @@ class Utilities {
 | 
			
		||||
        //Success
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Check whether a specified email address is valid or not
 | 
			
		||||
     *
 | 
			
		||||
     * @param mail The E-Mail address to check
 | 
			
		||||
     * @return True if the mail is valid / false else
 | 
			
		||||
     */
 | 
			
		||||
    boolean isValidMail(CharSequence mail){
 | 
			
		||||
        return !TextUtils.isEmpty(mail) && Patterns.EMAIL_ADDRESS.matcher(mail).matches();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -65,6 +65,7 @@
 | 
			
		||||
 | 
			
		||||
            <!-- Sign in button -->
 | 
			
		||||
            <Button
 | 
			
		||||
                android:id="@+id/login_submit"
 | 
			
		||||
                android:layout_width="match_parent"
 | 
			
		||||
                android:layout_height="wrap_content"
 | 
			
		||||
                android:layout_marginTop="8dp"
 | 
			
		||||
 
 | 
			
		||||
@@ -4,4 +4,6 @@
 | 
			
		||||
    <string name="activity_login_email_hint">Email address</string>
 | 
			
		||||
    <string name="activity_login_password_hint">Password</string>
 | 
			
		||||
    <string name="activity_login_submit_form">Login</string>
 | 
			
		||||
    <string name="activity_login_err_invalid_password">The password is invalid!</string>
 | 
			
		||||
    <string name="activity_login_err_invalid_email">The email specified is invalid !</string>
 | 
			
		||||
</resources>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user