Membuat Login Di Android Studio Dengan Mysql
Android Login And Registration Form Using Retrofit And Php
Tutorial android : Membuat login di android dengan php mysql
Pada bab ini, kita akan menjelaskan, bagaimana Anda bisa mengintegrasikan PHP dan MYSQL dengan aplikasi android Anda. Ini sangat berguna jika Anda memiliki server web, dan Anda ingin mengakses datanya di aplikasi android Anda. MYSQL digunakan sebagai database pada webserver dan PHP digunakan untuk mengambil data dari database. Aplikasi kami akan berkomunikasi dengan halaman PHP dengan parameter yang diperlukan dan PHP akan menghubungi database MYSQL dan akan mengambil hasilnya dan mengembalikan hasilnya kepada kami.
PHP – MYSQLMembuat Database
Database MYSQL bisa dibuat dengan mudah menggunakan script sederhana ini.
Pernyataan CREATE DATABASE menciptakan database
<?php $con=mysqli_connect("example.com","username","password"); $sql="CREATE DATABASE my_db"; if (mysqli_query($con,$sql)) { echo "Database my_db created successfully"; }?> |
Membuat Tabel Begitu database dibuat, saatnya membuat beberapa tabel di database.
Pernyataan CREATE TABLE
menciptakan database.
<?php $con=mysqli_connect("example.com","username","password","my_db"); $sql="CREATE TABLE table1(Username CHAR(30),Password CHAR(30),Role CHAR(30))"; if (mysqli_query($con,$sql)) { echo "Table have been created successfully"; }?> |
Memasukkan Nilai dalam tabelSaat database dan tabel dibuat.
Sekarang waktunya memasukkan beberapa data ke dalam tabel.
Pernyataan Insert Into menciptakan database.
<?php $con=mysqli_connect("example.com","username","password","my_db"); $sql="INSERT INTO table1 (FirstName, LastName, Age) VALUES ('admin', 'admin','adminstrator')"; if (mysqli_query($con,$sql)) { echo "Values have been inserted successfully"; }?> |
PHP – GET dan metode POSTPHP
juga digunakan untuk mengambil record dari database mysql begitu sudah dibuat. Agar bisa mengambil catatan beberapa informasi harus dilewatkan ke halaman PHP tentang catatan apa yang harus diambil. Metode pertama untuk menyampaikan informasi adalah melalui metode GET dimana perintah $ _GET digunakan. Variabel dilewatkan dalam url dan record diambil. Sintaksnya diberikan di bawah ini –
<?php $con=mysqli_connect("example.com","username","password","database name"); if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $username = $_GET['username']; $password = $_GET['password']; $result = mysqli_query($con,"SELECT Role FROM table1 where Username='$username' and Password='$password'"); $row = mysqli_fetch_array($result); $data = $row[0]; if($data){ echo $data; } mysqli_close($con);?> |
Metode kedua adalah menggunakan metode POST.
Satu-satunya perubahan pada skrip di atas adalah mengganti $ _GET dengan $ _POST. Dalam metode Post, variabel tidak dilewatkan melalui URL. Android – Menghubungkan MYSQLMenghubungkan Via Get MethodAda dua cara untuk terhubung ke MYSQL melalui halaman PHP. Yang pertama disebut metode Get. Kami akan menggunakan kelas HttpGet dan HttpClient untuk terhubung. Sintaksnya diberikan di bawah ini –
URL url = new URL(link);HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet();request.setURI(new URI(link)); |
Setelah itu Anda perlu memanggil metode eksekusi kelas HttpClient dan menerimanya di objek HttpResponse. Setelah itu Anda perlu membuka aliran untuk menerima data.
HttpResponse response = client.execute(request); BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); |
Menghubungkan Via Metode PostDalam metode Post, URLEncoder, kelas URLConnection akan digunakan. Urlencoder akan mengkodekan informasi dari variabel yang lewat. Sintaksnya diberikan di bawah ini –
URL url = new URL(link);String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");URLConnection conn = url.openConnection(); |
Hal terakhir yang perlu Anda lakukan adalah menulis data ini ke link. Setelah menulis, Anda perlu membuka aliran untuk menerima data yang merespons.
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write( data ); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
ContohContoh di bawah ini adalah contoh lengkap untuk menghubungkan aplikasi android Anda dengan database MYSQL melalui halaman PHP. Ini menciptakan aplikasi dasar yang memungkinkan Anda masuk menggunakan metode GET dan POST. PHP – MYSQL bagianDalam contoh ini database dengan nama temp telah dibuat di 000webhost.com. Dalam database tersebut, tabel telah dibuat dengan nama table1. Meja ini memiliki tiga bidang. (Username, Password, Role). Tabel hanya memiliki satu record yaitu (“admin”, “admin”, “administrator”).
Halaman php telah diberikan di bawah ini yang mengambil parameter oleh post method
<?php $con=mysqli_connect("mysql10.000webhost.com","username","password","db_name"); if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $username = $_POST['username']; $password = $_POST['password']; $result = mysqli_query($con,"SELECT Role FROM table1 where Username='$username' and Password='$password'"); $row = mysqli_fetch_array($result); $data = $row[0]; if($data){ echo $data; } mysqli_close($con);?> |
Inilah isi dari src/com.example.phpmysql/MainActivity.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <span class="kwd">package</span><span class="pln"> com</span><span class="pun">.</span><span class="pln">example</span><span class="pun">.</span><span class="pln">phpmysql</span><span class="pun">;</span> <span class="kwd">import</span><span class="pln"> android</span><span class="pun">.</span><span class="pln">app</span><span class="pun">.</span><span class="typ">Activity</span><span class="pun">;</span> <span class="kwd">import</span><span class="pln"> android</span><span class="pun">.</span><span class="pln">os</span><span class="pun">.</span><span class="typ">Bundle</span><span class="pun">;</span> <span class="kwd">import</span><span class="pln"> android</span><span class="pun">.</span><span class="pln">view</span><span class="pun">.</span><span class="typ">View</span><span class="pun">;</span> <span class="kwd">import</span><span class="pln"> android</span><span class="pun">.</span><span class="pln">widget</span><span class="pun">.</span><span class="typ">EditText</span><span class="pun">;</span> <span class="kwd">import</span><span class="pln"> android</span><span class="pun">.</span><span class="pln">widget</span><span class="pun">.</span><span class="typ">TextView</span><span class="pun">;</span> <span class="kwd">public</span> <span class="kwd">class</span> <span class="typ">MainActivity</span> <span class="kwd">extends</span> <span class="typ">Activity</span> <span class="pun">{</span> <span class="kwd">private</span> <span class="typ">EditText</span><span class="pln"> usernameField</span><span class="pun">,</span><span class="pln">passwordField</span><span class="pun">;</span> <span class="kwd">private</span> <span class="typ">TextView</span><span class="pln"> status</span><span class="pun">,</span><span class="pln">role</span><span class="pun">,</span><span class="pln">method</span><span class="pun">;</span> <span class="lit">@Override</span> <span class="kwd">protected</span> <span class="kwd">void</span><span class="pln"> onCreate</span><span class="pun">(</span><span class="typ">Bundle</span><span class="pln"> savedInstanceState</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">super</span><span class="pun">.</span><span class="pln">onCreate</span><span class="pun">(</span><span class="pln">savedInstanceState</span><span class="pun">);</span><span class="pln"> setContentView</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">layout</span><span class="pun">.</span><span class="pln">activity_main</span><span class="pun">);</span><span class="pln"> usernameField </span><span class="pun">=</span> <span class="pun">(</span><span class="typ">EditText</span><span class="pun">)</span><span class="pln">findViewById</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">id</span><span class="pun">.</span><span class="pln">editText1</span><span class="pun">);</span><span class="pln"> passwordField </span><span class="pun">=</span> <span class="pun">(</span><span class="typ">EditText</span><span class="pun">)</span><span class="pln">findViewById</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">id</span><span class="pun">.</span><span class="pln">editText2</span><span class="pun">);</span><span class="pln"> status </span><span class="pun">=</span> <span class="pun">(</span><span class="typ">TextView</span><span class="pun">)</span><span class="pln">findViewById</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">id</span><span class="pun">.</span><span class="pln">textView6</span><span class="pun">);</span><span class="pln"> role </span><span class="pun">=</span> <span class="pun">(</span><span class="typ">TextView</span><span class="pun">)</span><span class="pln">findViewById</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">id</span><span class="pun">.</span><span class="pln">textView7</span><span class="pun">);</span><span class="pln"> method </span><span class="pun">=</span> <span class="pun">(</span><span class="typ">TextView</span><span class="pun">)</span><span class="pln">findViewById</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">id</span><span class="pun">.</span><span class="pln">textView9</span><span class="pun">);</span> <span class="pun">}</span> <span class="kwd">public</span> <span class="kwd">void</span><span class="pln"> login</span><span class="pun">(</span><span class="typ">View</span><span class="pln"> view</span><span class="pun">){</span> <span class="typ">String</span><span class="pln"> username </span><span class="pun">=</span><span class="pln"> usernameField</span><span class="pun">.</span><span class="pln">getText</span><span class="pun">().</span><span class="pln">toString</span><span class="pun">();</span> <span class="typ">String</span><span class="pln"> password </span><span class="pun">=</span><span class="pln"> passwordField</span><span class="pun">.</span><span class="pln">getText</span><span class="pun">().</span><span class="pln">toString</span><span class="pun">();</span><span class="pln"> method</span><span class="pun">.</span><span class="pln">setText</span><span class="pun">(</span><span class="str">"Get Method"</span><span class="pun">);</span> <span class="kwd">new</span> <span class="typ">SigninActivity</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">,</span><span class="pln">status</span><span class="pun">,</span><span class="pln">role</span><span class="pun">,</span><span class="lit">0</span><span class="pun">).</span><span class="pln">execute</span><span class="pun">(</span><span class="pln">username</span><span class="pun">,</span><span class="pln">password</span><span class="pun">);</span> <span class="pun">}</span> <span class="kwd">public</span> <span class="kwd">void</span><span class="pln"> loginPost</span><span class="pun">(</span><span class="typ">View</span><span class="pln"> view</span><span class="pun">){</span> <span class="typ">String</span><span class="pln"> username </span><span class="pun">=</span><span class="pln"> usernameField</span><span class="pun">.</span><span class="pln">getText</span><span class="pun">().</span><span class="pln">toString</span><span class="pun">();</span> <span class="typ">String</span><span class="pln"> password </span><span class="pun">=</span><span class="pln"> passwordField</span><span class="pun">.</span><span class="pln">getText</span><span class="pun">().</span><span class="pln">toString</span><span class="pun">();</span><span class="pln"> method</span><span class="pun">.</span><span class="pln">setText</span><span class="pun">(</span><span class="str">"Post Method"</span><span class="pun">);</span> <span class="kwd">new</span> <span class="typ">SigninActivity</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">,</span><span class="pln">status</span><span class="pun">,</span><span class="pln">role</span><span class="pun">,</span><span class="lit">1</span><span class="pun">).</span><span class="pln">execute</span><span class="pun">(</span><span class="pln">username</span><span class="pun">,</span><span class="pln">password</span><span class="pun">);</span> <span class="pun">}</span> <span class="pun">}</span> |
Isi Dari Sign dari src/com.example.phpmysql/SigninActivity.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | package com.example.phpmysql; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URI; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.content.Context; import android.os.AsyncTask; import android.widget.TextView; public class SigninActivity extends AsyncTask{ private TextView statusField,roleField; private Context context; private int byGetOrPost = 0; //flag 0 means get and 1 means post.(By default it is get.) public SigninActivity(Context context,TextView statusField,TextView roleField,int flag) { this.context = context; this.statusField = statusField; this.roleField = roleField; byGetOrPost = flag; } protected void onPreExecute(){ } @Override protected String doInBackground(String... arg0) { if(byGetOrPost == 0){ //means by Get Method try{ String username = (String)arg0[0]; String password = (String)arg0[1]; String link = "http://myphpmysqlweb.hostei.com/login.php?username="+username+"& password="+password; URL url = new URL(link); HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(); request.setURI(new URI(link)); HttpResponse response = client.execute(request); BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer sb = new StringBuffer(""); String line=""; while ((line = in.readLine()) != null) { sb.append(line); break; } in.close(); return sb.toString(); } catch(Exception e){ return new String("Exception: " + e.getMessage()); } } else{ try{ String username = (String)arg0[0]; String password = (String)arg0[1]; String link="http://myphpmysqlweb.hostei.com/loginpost.php"; String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8"); data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8"); URL url = new URL(link); URLConnection conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write( data ); wr.flush(); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; // Read Server Response while((line = reader.readLine()) != null) { sb.append(line); break; } return sb.toString(); } catch(Exception e){ return new String("Exception: " + e.getMessage()); } } } @Override protected void onPostExecute(String result){ this.statusField.setText("Login Successful"); this.roleField.setText(result); } } |
Isi dari gradle
android { useLibrary 'org.apache.http.legacy' } |
Isi dari activity_main.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | <?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=".MainActivity" > <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/editText1" android:layout_below="@+id/editText1" android:layout_marginTop="25dp" android:ems="10" android:inputType="textPassword" > </EditText> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="44dp" android:ems="10" > <requestFocus android:layout_width="wrap_content" /> </EditText> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/editText1" android:layout_alignParentLeft="true" android:text="@string/Username" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="@string/App" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView5" android:layout_alignLeft="@+id/textView6" android:text="@string/Role" android:textAppearance="?android:attr/textAppearanceMedium" android:textSize="10sp" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView6" android:layout_marginTop="27dp" android:layout_toLeftOf="@+id/editText1" android:text="@string/LoginRole" /> <TextView android:id="@+id/textView8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/textView6" android:layout_alignLeft="@+id/textView5" android:layout_marginBottom="27dp" android:text="@string/method" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView8" android:layout_below="@+id/button1" android:layout_marginTop="86dp" android:text="@string/LoginStatus" /> <TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/textView4" android:layout_centerHorizontal="true" android:text="@string/Status" android:textAppearance="?android:attr/textAppearanceMedium" android:textSize="10sp" /> <TextView android:id="@+id/textView9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView8" android:layout_alignLeft="@+id/textView6" android:text="@string/Choose" android:textAppearance="?android:attr/textAppearanceMedium" android:textSize="10sp" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/textView6" android:onClick="loginPost" android:text="@string/LoginPost" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button2" android:layout_alignBottom="@+id/button2" android:layout_alignLeft="@+id/textView2" android:onClick="login" android:text="@string/LoginGet" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/editText2" android:layout_alignBottom="@+id/editText2" android:layout_alignParentLeft="true" android:text="@string/Password" /> </RelativeLayout> |
Strings.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">PHPMYSQL</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="Username">Username</string> <string name="Password">Password</string> <string name="LoginGet">Login - Get</string> <string name="LoginPost">Login - Post</string> <string name="App">Login Application</string> <string name="LoginStatus">Login Status</string> <string name="LoginRole">Login Role</string> <string name="Status">Not login</string> <string name="Role">Not assigned</string> <string name="method">Login Method</string> <string name="Choose">Choose Method</string> </resources> |
AndroidManifest.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.phpmysql" > <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.phpmysql.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
Mari coba jalankan aplikasi PHPMYSQL Anda. Saya berasumsi bahwa Anda telah menghubungkan perangkat Android Mobile Anda yang sebenarnya dengan komputer Anda. Untuk menjalankan aplikasi dari studio Android, buka salah satu file aktivitas proyek Anda dan klik ikon Run Eclipse Run Icon dari panel alat. Sebelum memulai aplikasi Anda, studio Android akan menampilkan jendela berikut untuk memilih opsi di mana Anda ingin menjalankan aplikasi Android Anda
Sampai disn dulu ya tutorial android “membuat login di android” silahkan komentar Jika Ada yang Ingn di tanyakan
Gallery Membuat Login Di Android Studio Dengan Mysql
Restful Crud With Java And Mysql In Minutes
Android Login With Google Tutorial
Belajar Android Php Mysql Login Dengan Volley Blog Belajar
Android Firebase How To Make User Registration And Login With Email And Password In Android Studio
Aplikasi Login Sederhana Android Studio Tanpa Database
Membuat Aplikasi Android Pertama Dengan Android Studio
Simple Ionic Login With Angular 2 Devdactic
How To Make Login In Android Use Mysql Server Membuat
Android Login And Register With Sqlite Database Tutorial
Building An Awesome Login Screen With Nativescript
Android Pie Chart How To Create Pie Chart In Android
Membuat Login Sederhana Android Studio Youtube
Geofencing Api Tutorial For Android Raywenderlich Com
Login Form Example In Kotlin Android
Starting Android Development Creating A Todo App
Make Login In Android App Use Mysql Database Android Studio
How To Implement Mobile To Web Cross Login Using A Qr Code
Android Login With Google Tutorial
Cara Set Data Spinner Dari Json Api Server Retrofit Android
Android Login And Register Using Restful Web Services Java
Register And Login Using Sqlite In Xamarin Forms Dzone
0 Response to "Membuat Login Di Android Studio Dengan Mysql"
Post a Comment