Cara Koneksi database Mysql & PHP dengan Android Eclipse
Cara Koneksi database Mysql & PHP dengan Android Eclipse - Melakukan connect databse Mysql ke Eclipse Android Developer dengan PHP untuk penghubung antara Java dengan Mysql seperti Gambar dibawah ini.
========================================================================
package com.example.koneksi;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.library.DatabaseHandler;
import com.example.library.UserFunctions;
public class KoneksiActivity extends Activity {
Button btnLogin;
EditText inputEmail;
EditText inputPassword;
TextView loginErrorMsg;
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.koneksi);
// Importing all assets like buttons, text fields
inputEmail = (EditText) findViewById(R.id.loginEmail);
inputPassword = (EditText) findViewById(R.id.loginPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
loginErrorMsg = (TextView) findViewById(R.id.login_error);
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
Log.d("Button", "Login");
JSONObject json = userFunction.loginUser(email, password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
loginErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Login Screen
finish();
}else{
// Error in login
loginErrorMsg.setText("Inputkan koneksi yang benar");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
========================================================================
Persiapkan pada aplikasi koneksi database telah selesai tinggal kita buat cara mengkoneksikan antara Java dengan Mysql dengan PHP, langkahnya aktifkan XAMP lalu ke PhpMyadmin lalu Create Database dengan nama Db_Eclipse lalu import file SQL berikut ini :
DOWNLOAD FILE SQL DB_Eclipse
Setelah sudah di download create folder HTDOCS dengan nama koneksi_eclipse dan download file lengkapnya pada link dibawah ini :
DOWNLOAD FILE PHP ECLIPSE koneksi_Eclipse
dan kalian coba untuk di Running dengan Emulator android virtual atau smartphone android kalian , bila sukse maka hasilnya akan seperti gambar dibawah ini :
keterangan pada saat Running Program dia akan meminta akses user :
Langsung kita bahas untuk tahapan berikutnya perhatikan baik-baik, jangan skip bacaan bila belum sepenuhnya mengerti dan baca berulang kali
1. buat Project baru Andrid dengan cara FILE > Android Application Project> Application Name "Koneksi" selanjutnya next seusai pengaturan default
2. Hapus Main.XML berada pada folder res > Layout lalu hapus MainActivity.JAVA berada pada folder scr > com.example.koneksi (di hapus karena kali ini tidak membutuhkannya)
3. Buat folder com.example.library pada folder scr , seperti berikut ini struktur foldernya .
4. Setelah folder dibuat lalu dibuat Activity terlebih dahulu, pertama create DashboardActivity.JAVA dengan cara klik kanan pada folder com.example.koneksi > new > class, berikan namnay DashboardActivity dan copy source code berikut ini :
========================================================================
* Author: Berga
* Editor: Septian Maulana
* URL: www.berga-template.blogspot.com
*/
package com.example.koneksi;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.library.DatabaseHandler;
import com.example.library.UserFunctions;
public class KoneksiActivity extends Activity {
Button btnLogin;
EditText inputEmail;
EditText inputPassword;
TextView loginErrorMsg;
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.koneksi);
// Importing all assets like buttons, text fields
inputEmail = (EditText) findViewById(R.id.loginEmail);
inputPassword = (EditText) findViewById(R.id.loginPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
loginErrorMsg = (TextView) findViewById(R.id.login_error);
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
Log.d("Button", "Login");
JSONObject json = userFunction.loginUser(email, password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
loginErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Login Screen
finish();
}else{
// Error in login
loginErrorMsg.setText("Inputkan koneksi yang benar");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
========================================================================
Setelah itu car yang sama buat Class baru dengan nama "KoneksiActivity" lalu copy source code berikut ini "KoneksiActivity.java"
========================================================================
* Author: Berga
* Editor: Septian Maulana
* URL: www.berga-template.blogspot.com
*/
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.library.DatabaseHandler;
import com.example.library.UserFunctions;
public class KoneksiActivity extends Activity {
Button btnLogin;
EditText inputEmail;
EditText inputPassword;
TextView loginErrorMsg;
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.koneksi);
// Importing all assets like buttons, text fields
inputEmail = (EditText) findViewById(R.id.loginEmail);
inputPassword = (EditText) findViewById(R.id.loginPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
loginErrorMsg = (TextView) findViewById(R.id.login_error);
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
Log.d("Button", "Login");
JSONObject json = userFunction.loginUser(email, password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
loginErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Login Screen
finish();
}else{
// Error in login
loginErrorMsg.setText("Inputkan koneksi yang benar");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
========================================================================
Persiapkan pada aplikasi koneksi database telah selesai tinggal kita buat cara mengkoneksikan antara Java dengan Mysql dengan PHP, langkahnya aktifkan XAMP lalu ke PhpMyadmin lalu Create Database dengan nama Db_Eclipse lalu import file SQL berikut ini :
DOWNLOAD FILE SQL DB_Eclipse
Setelah sudah di download create folder HTDOCS dengan nama koneksi_eclipse dan download file lengkapnya pada link dibawah ini :
DOWNLOAD FILE PHP ECLIPSE koneksi_Eclipse
dan kalian coba untuk di Running dengan Emulator android virtual atau smartphone android kalian , bila sukse maka hasilnya akan seperti gambar dibawah ini :
keterangan pada saat Running Program dia akan meminta akses user :
- username :admin
- Password : admin
untuk mendapatkan Tutorial lengkap dan Source code lebih banyak bisa download dibawah ini :
Comments
Post a Comment
-Berkomentarlah yang baik dan rapi.
-Menggunakan link aktif akan dihapus.