Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android dan database 2 M. Taufiq, M. Kom.

Similar presentations


Presentation on theme: "Android dan database 2 M. Taufiq, M. Kom."— Presentation transcript:

1 Android dan database 2 M. Taufiq, M. Kom

2 Android dan Database server
Yang dibutuhkan dalam pembuatan. Sebuah PC/LAPTOP (difungsikan sebagai server) Sebuah koneksi (WIFI hotspot) MySQL server (Xampp, lamp dll) Handphone PHP editor (notepad, sublime, dll)

3

4 Yang akan kita buat Install server mysql database dan apache di laptop (Xampp) Buat database di mysql (database login) Membuat file eksekusi dengan php (login.php) Menambahkan dependencies (bulid.gradle (modul:app)) Membuat program untuk mobile

5 database CREATE DATABASE login
CREATE TABLE IF NOT EXISTS users ( id int(4) NOT NULL, username varchar(32) NOT NULL, password varchar(64) NOT NULL, varchar(32) NOT NULL, accountname varchar(32) NOT NULL, status int(4) NOT NULL DEFAULT '1' ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; ALTER TABLE users ADD PRIMARY KEY (id); INSERT INTO users (id, username, password, , accountname, status) VALUES (1, 'admin', 'abcd1234', 'Admin', 1);

6 login.php <?php define('HOST','localhost'); define('USER','root'); define('PASS',''); define('DB',‘login'); $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect'); if($_SERVER['REQUEST_METHOD']=='POST'){ //Getting values $username = $_POST[' ']; $password = $_POST['password']; //Creating sql query $sql = "SELECT * FROM users WHERE ='$username' AND password='$password'"; //executing query $result = mysqli_query($con,$sql); //fetching result $check = mysqli_fetch_array($result); //if we got some result if(isset($check)){ //displaying success echo "success"; }else{ //displaying failure echo "failure"; } mysqli_close($con); ?>

7 App.gradle dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.2.0' compile 'com.android.support:design:23.2.0' compile 'com.mcxiaoke.volley:library-aar:1.0.0' }

8 Main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent“ android:orientation="vertical" > <ImageView android:layout_gravity="center_horizontal" android:layout_width="150dp" android:layout_height="150dp" /> <android.support.design.widget.TextInputLayout android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp"> <EditText android:layout_width="match_parent“ android:inputType="text Address" android:hint=" " /> </android.support.design.widget.TextInputLayout> <EditText android:inputType="textPassword" android:hint="Password"/> <android.support.v7.widget.AppCompatButton android:layout_width="fill_parent" android:layout_marginTop="24dp" android:layout_marginBottom="24dp" android:padding="12dp" android:text="Login"/> </LinearLayout>

9 import android. app. ProgressDialog; import android. content
import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.AppCompatButton; import android.view.View; import android.widget.EditText; import android.widget.Toast; import com.android.volley.AuthFailureError; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import java.util.HashMap; import java.util.Map; public class MainActivity extends AppCompatActivity { //Defining views private EditText editText ; private EditText editTextPassword; private Context context; private AppCompatButton buttonLogin; private ProgressDialog protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = MainActivity.this; //Initializing views pDialog = new ProgressDialog(context); editText = (EditText) findViewById(R.id.editText ); editTextPassword = (EditText) findViewById(R.id.editTextPassword); buttonLogin = (AppCompatButton) findViewById(R.id.buttonLogin); //Adding click listener buttonLogin.setOnClickListener(new View.OnClickListener() public void onClick(View v) { login(); } }); //editTextPassword.setText("abcd1234"); } private void login() { //Getting values from edit texts final String = editText .getText().toString().trim(); final String password = editTextPassword.getText().toString().trim(); pDialog.setMessage("Login Process..."); showDialog(); //Creating a string request StringRequest stringRequest = new StringRequest(Request.Method.POST, AppVar.LOGIN_URL, new Response.Listener<String>() public void onResponse(String response) { //If we are getting success from server if (response.contains(AppVar.LOGIN_SUCCESS)) { hideDialog(); gotoCourseActivity(); } else { hideDialog(); //Displaying an error message on toast Toast.makeText(context, "Invalid username or password", Toast.LENGTH_LONG).show(); } } }, new Response.ErrorListener() public void onErrorResponse(VolleyError error) { //You can handle error here if you want hideDialog(); Toast.makeText(context, "The server unreachable", Toast.LENGTH_LONG).show(); } }) protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> params = new HashMap<>(); //Adding parameters to request params.put(AppVar.KEY_ , ); params.put(AppVar.KEY_PASSWORD, password); //returning parameter return params; } }; //Adding the string request to the queue Volley.newRequestQueue(this).add(stringRequest); } private void gotoCourseActivity() { Intent intent = new Intent(context, CourseActivity.class); startActivity(intent); finish(); } private void showDialog() { if (!pDialog.isShowing()) pDialog.show(); } private void hideDialog() { if (pDialog.isShowing()) pDialog.dismiss(); } }

10 Appvar.java public class AppVar { //URL to our login.php file, url bisa diganti sesuai dengan alamat server kita public static final String LOGIN_URL = " //Keys for and password as defined in our $_POST['key'] in login.php public static final String KEY_ = " "; public static final String KEY_PASSWORD = "password"; //If server response is equal to this that means login is successful public static final String LOGIN_SUCCESS = "success"; }

11 Activity_course.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".CourseActivity"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> </RelativeLayout>

12 Androidmanifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=" package=“nama package"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:supportsRtl="true" <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".CourseActivity" android:label="agusharyanto.net"> </application> </manifest>


Download ppt "Android dan database 2 M. Taufiq, M. Kom."

Similar presentations


Ads by Google