كيفاش تستعمل Volley باش ترسل البيانات الجزء الأول
فهاد الدرس من سلسلة الاندرويد غادي نشوفو كيفاش نستعملو ل volley library باش نديرو إتصال مع قاعدة بيانات ف serveur غادي نقادو واحد التطبيق لي غادي يمكن المستخدم من التسجيل فقاعدة البيانات و منبعد يمكن يدخل بإستعمال المعلومات لي سجل كما يمكنلو يعدل أو يمسح الحساب ديالو.
نظرة سريعة بالفيديو
- إضافة ل Volley library للمشروع
أول حاجة غادي نديرو غادي نفتحو Android Studio وغادي نزيدو مشروع جديد يمكن تسميه لي بغيتي انا سميتو RestApp من بعد غادي تمشي للمجلد Gradle Scripts و تدخل للملف build.gradle لي غادي يعطيك الملف app منبعد غادي تزيد فيه هاد السطر compile com.mcxiaoke.volley:library:1.0.19 ف dependencies لي غادي يمكنك من إضافة volley library للمشروع ديالنا الكود ديال هاد الملف هو:
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.darijacoding.restapp"
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.mcxiaoke.volley:library:1.0.19'
}
- الملف activity_login.xml
غادي تزيد هاد الكود باش تمكن التطبيق من الإتصال بالإنترنيت ف AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".LoginActivity"
android:fitsSystemWindows="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="56dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<!-- Email Label -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="78dp"
android:layout_marginBottom="8dp">
<EditText android:id="@+id/input_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Email" />
</LinearLayout>
<!-- Password Label -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<EditText android:id="@+id/input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Mot de passe"/>
</LinearLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginBottom="24dp"
android:padding="12dp"
android:background="@color/colorPrimary"
android:textColor="#FFFFFF"
android:text="Connexion"/>
<TextView
android:id="@+id/link_signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="Nouveau utilisateur ? inscrivez vous"
android:textColor="@color/colorPrimary"
android:gravity="center"
android:textSize="16sp"/>
</LinearLayout>
</FrameLayout>
- الملف activity_register.xml
دائما فالمجلد res/layout غادي نزيد ملف جديد نسميه activity_register.xml هاد الملف غادي تكون فيه ل Form لي غادي تمكن المستخدم من التسجيل فقاعدة البيانات لي كتضمن الإسم والبريد الإلكتروني وكلمة المرور ولي الكود ديالو هو :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".RegisterActivity"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:paddingTop="100dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<EditText
android:id="@+id/NomEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:hint="Nom & Prénom" />
</LinearLayout>
<!-- Password Label -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<EditText android:id="@+id/input_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Email" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<EditText android:id="@+id/input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Mot de passe"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="18dp">
<Button
android:id="@+id/btn_signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="@color/colorPrimary"
android:textColor="#FFFFFF"
android:layout_marginBottom="24dp"
android:padding="12dp"
android:textAllCaps="false"
android:text="Créer compte"/>
<TextView
android:id="@+id/link_login"
android:layout_width="fill_parent"
android:textColor="@color/colorPrimary"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:text="Déja membre ? Connectez vous"
android:gravity="center"
android:textSize="16dip"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
- الملف activity_update.xml
دائما فالمجلد res/layout غادي نزيد ملف جديد نسميه activity_update.xml هاد الملف غادي تكون فيه ل Form لي غادي تمكن المستخدم من تعديل المعلومات الخاصة به فقاعدة البيانات لي كتضمن الإسم والبريد الإلكتروني وكلمة المرور ولي الكود ديالو هو :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".RegisterActivity"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:paddingTop="100dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<EditText
android:id="@+id/NomEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:hint="Nom & Prénom" />
</LinearLayout>
<!-- Password Label -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<EditText android:id="@+id/input_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Email" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<EditText android:id="@+id/input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Mot de passe"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="18dp">
<Button
android:id="@+id/btn_update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="@color/colorPrimary"
android:textColor="#FFFFFF"
android:layout_marginBottom="24dp"
android:padding="12dp"
android:textAllCaps="false"
android:text="Modifier compte"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
- الملف activity_profile.xml
دائما فالمجلد res/layout غادي نزيد ملف جديد نسميه activity_profile.xml هاد الملف غادي تكون فيه الأدوات لي غادي تمكن المستخدم من مشاهدة أو تعديل المعلومات الخاصة به فقاعدة البيانات لي كتضمن الإسم والبريد الإلكتروني وكلمة المرور كما كتمكنو من حذف الحساب ديالو ولي الكود ديالو هو :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.darijacoding.restapp.model.ProfileActivity">
<LinearLayout
android:id="@+id/userInfoLayout"
android:orientation="vertical"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:text="Nom"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/email"
android:layout_marginTop="30dp"
android:text="Email"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/actionsLayout"
android:orientation="vertical"
android:layout_margin="20dp"
android:layout_below="@+id/userInfoLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/updateBtn"
android:text="Modifier mon compte"
android:textSize="15sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/deleteBtn"
android:text="Supprimer mon compte"
android:textSize="15sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/LogoutBtn"
android:text="Déconnexion"
android:textSize="15sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>