Advertisement
zza_ibliizt

LoginDatabase login.java

Dec 19th, 2018
1,283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. package com.example.logindatabase;
  2.  
  3. import java.util.ArrayList;
  4. import org.apache.http.NameValuePair;
  5. import org.apache.http.message.BasicNameValuePair;
  6. import Class.Connection;
  7. import Class.CustomHttpClient;
  8.  
  9. import android.os.Bundle;
  10. import android.app.Activity;
  11. import android.content.Intent;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.Toast;
  17.  
  18. public  class login extends Activity implements OnClickListener {
  19.  
  20.     EditText username, password;
  21.     Button login, exit;
  22.    
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.login);
  27.        
  28.         username    = (EditText)findViewById(R.id.edittext_username);
  29.         password    = (EditText)findViewById(R.id.edittext_password);
  30.        
  31.         login       = (Button)findViewById(R.id.button_login);
  32.         login.setOnClickListener(this);
  33.        
  34.         exit        = (Button)findViewById(R.id.button_exit);
  35.         exit.setOnClickListener(this);
  36.     }
  37.  
  38.     @Override
  39.     public void onClick(View v) {
  40.         // Button Login
  41.         if (v == login){
  42.            
  43.             //=======================================================================
  44.             // Ini untuk kirim Parameter yang diinput username & password ke PHP
  45.             //=======================================================================
  46.            
  47.             ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
  48.             postParameters.add(new BasicNameValuePair("post_username", username.getText().toString()));
  49.             postParameters.add(new BasicNameValuePair("post_password", password.getText().toString()));
  50.            
  51.             String response = null;
  52.            
  53.             //=======================================================================
  54.             // Ini untuk koneksi ke Database MySQL
  55.             //=======================================================================
  56.             try {
  57.                
  58.                 //===================================================================
  59.                 // IP       : dari Class Connection (IP localhost 10.0.2.2)
  60.                 // URL_login: dari Class Connection, untuk mengakses lokasi PHP
  61.                 //===================================================================          
  62.                 response = CustomHttpClient.executeHttpPost(Connection.IP + Connection.URL_login, postParameters);
  63.                 String res = response.toString();
  64.                 res = res.trim();
  65.                 res = res.replaceAll("\\s+", "");
  66.                
  67.                 // ini untuk notif jika koneksi tidak berhasil
  68.                 Toast.makeText(login.this, res, Toast.LENGTH_LONG).show();
  69.                
  70.                 // String valid = "1";
  71.                 // Jika Berhasil data valid    
  72.                 if (res.equals("1")) {
  73.                    
  74.                     Intent i = new Intent(login.this, home.class);
  75.                     startActivity(i);
  76.                 }
  77.                 else {
  78.                     Toast.makeText(login.this, "Gagal Login Gan!", Toast.LENGTH_LONG).show();
  79.                 }
  80.                
  81.             } catch (Exception e) {
  82.                 Toast.makeText(login.this, "Kesalahan di Koneksinya Gan!", Toast.LENGTH_LONG).show();
  83.             }      
  84.         }
  85.        
  86.         // Button Exit
  87.         else if (v == exit) {
  88.             finish();
  89.             System.exit(0);
  90.         }  
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement