Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.logindatabase;
- import java.util.ArrayList;
- import org.apache.http.NameValuePair;
- import org.apache.http.message.BasicNameValuePair;
- import Class.Connection;
- import Class.CustomHttpClient;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Intent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class login extends Activity implements OnClickListener {
- EditText username, password;
- Button login, exit;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.login);
- username = (EditText)findViewById(R.id.edittext_username);
- password = (EditText)findViewById(R.id.edittext_password);
- login = (Button)findViewById(R.id.button_login);
- login.setOnClickListener(this);
- exit = (Button)findViewById(R.id.button_exit);
- exit.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- // Button Login
- if (v == login){
- //=======================================================================
- // Ini untuk kirim Parameter yang diinput username & password ke PHP
- //=======================================================================
- ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
- postParameters.add(new BasicNameValuePair("post_username", username.getText().toString()));
- postParameters.add(new BasicNameValuePair("post_password", password.getText().toString()));
- String response = null;
- //=======================================================================
- // Ini untuk koneksi ke Database MySQL
- //=======================================================================
- try {
- //===================================================================
- // IP : dari Class Connection (IP localhost 10.0.2.2)
- // URL_login: dari Class Connection, untuk mengakses lokasi PHP
- //===================================================================
- response = CustomHttpClient.executeHttpPost(Connection.IP + Connection.URL_login, postParameters);
- String res = response.toString();
- res = res.trim();
- res = res.replaceAll("\\s+", "");
- // ini untuk notif jika koneksi tidak berhasil
- Toast.makeText(login.this, res, Toast.LENGTH_LONG).show();
- // String valid = "1";
- // Jika Berhasil data valid
- if (res.equals("1")) {
- Intent i = new Intent(login.this, home.class);
- startActivity(i);
- }
- else {
- Toast.makeText(login.this, "Gagal Login Gan!", Toast.LENGTH_LONG).show();
- }
- } catch (Exception e) {
- Toast.makeText(login.this, "Kesalahan di Koneksinya Gan!", Toast.LENGTH_LONG).show();
- }
- }
- // Button Exit
- else if (v == exit) {
- finish();
- System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement