Advertisement
heru-kzw

Login Json

May 22nd, 2017
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.10 KB | None | 0 0
  1. package com.penginapan.penginapan;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.os.AsyncTask;
  5. import android.os.Bundle;
  6. import android.app.Activity;
  7. import android.app.AlertDialog;
  8. import android.content.DialogInterface;
  9. import android.content.Intent;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.util.Log;
  12. import android.view.KeyEvent;
  13. import android.view.View;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import org.apache.http.NameValuePair;
  17. import org.apache.http.message.BasicNameValuePair;
  18. import org.json.JSONArray;
  19. import org.json.JSONException;
  20. import org.json.JSONObject;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23.  
  24. public class Login extends AppCompatActivity {
  25. String xml="";
  26. EditText txtpassword,txtusername;
  27.  
  28.     private ProgressDialog pDialog;
  29.     JSONParser jsonParser = new JSONParser();
  30.  
  31.     private static final String TAG_SUKSES = "sukses";
  32.     private static final String TAG_record = "record";
  33.     private static final String TAG_xmlbio= "xmlbio";
  34.  
  35.     String xmlbio="";
  36.     String ip="";
  37.  
  38.     String username,password;
  39.     @Override
  40.     protected void onCreate(Bundle savedInstanceState) {
  41.         super.onCreate(savedInstanceState);
  42.         setContentView(R.layout.login);
  43.         ip=jsonParser.getIP();
  44.  
  45.         txtusername=(EditText)findViewById(R.id.edUser);
  46.         txtpassword=(EditText)findViewById(R.id.edPass);
  47.  
  48.         Button btnLogin=(Button)findViewById(R.id.btnLogin);
  49.         btnLogin.setOnClickListener(new View.OnClickListener() {
  50.         public void onClick(View arg0) {
  51.  
  52.             username=txtusername.getText().toString().trim();
  53.             password=txtpassword.getText().toString().trim();
  54.  
  55.             if(username.length()<1){lengkapi("username");}
  56.         else if(password.length()<1){lengkapi("password");}
  57.         else {
  58.  
  59.                 username = username.replaceAll(" ", "_");
  60.                 password = password.replaceAll(" ", "_");
  61.  
  62.                 new ceklogin().execute();
  63.  
  64.             }
  65.     }});
  66.  
  67.  
  68.         Button btnRegistrasi=(Button)findViewById(R.id.btnRegistrasi);
  69.         btnRegistrasi.setOnClickListener(new View.OnClickListener() {
  70.         public void onClick(View arg0) {
  71.                 Intent i = new Intent(Login.this,Registrasi.class);
  72.             i.putExtra("pk", "");
  73.  
  74.                   startActivity(i);
  75.     }});
  76.  
  77.  
  78.         Button btnForgot=(Button)findViewById(R.id.btnForgot);
  79.         btnForgot.setOnClickListener(new View.OnClickListener() {
  80.         public void onClick(View arg0) {
  81.                 Intent i = new Intent(Login.this,Forgot.class);
  82.                   startActivity(i);
  83.     }});
  84.  
  85.     }
  86.  
  87.  
  88.     class ceklogin extends AsyncTask<String, String, String> {
  89.         @Override
  90.         protected void onPreExecute() {
  91.             super.onPreExecute();
  92.             pDialog = new ProgressDialog(Login.this);
  93.             pDialog.setMessage("Proses Login...");
  94.             pDialog.setIndeterminate(false);
  95.             pDialog.setCancelable(true);
  96.             pDialog.show();
  97.         }
  98.         protected String doInBackground(String... params) {
  99.             int sukses;
  100.             try {
  101.                 List<NameValuePair> myparams = new ArrayList<NameValuePair>();
  102.                 myparams.add(new BasicNameValuePair("username", username));
  103.                 myparams.add(new BasicNameValuePair("password", password));
  104.  
  105.                 String url=ip+"android/login.php";
  106.                 Log.v("detail",url);
  107.                 JSONObject json = jsonParser.makeHttpRequest(url, "GET", myparams);
  108.                 Log.d("detail", json.toString());
  109.                 sukses = json.getInt(TAG_SUKSES);
  110.                 if (sukses == 1) {
  111.                     JSONArray myObj = json.getJSONArray(TAG_record); // JSON Array
  112.                     final JSONObject myJSON = myObj.getJSONObject(0);
  113.                     runOnUiThread(new Runnable() {
  114.                         public void run() {
  115.                             try {
  116.                                 xmlbio=myJSON.getString(TAG_xmlbio);
  117.  
  118.                                 Log.d("xmlbio", xmlbio);
  119.  
  120.                                 Intent i = new Intent(getApplicationContext(),Menu_pengusaha.class);
  121.                                 i.putExtra("xmlbio", xmlbio);
  122.                                 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  123.                                 startActivity(i);
  124.                             }
  125.                             catch (JSONException e) {e.printStackTrace();}
  126.                         }});
  127.                 }
  128.                 else{
  129.                     // jika id tidak ditemukan
  130.                 }
  131.             } catch (JSONException e) {
  132.                 e.printStackTrace();
  133.             }
  134.             return null;
  135.         }
  136.         protected void onPostExecute(String file_url) {pDialog.dismiss();}
  137.     }
  138.  
  139.  
  140.         public void lengkapi(String item){
  141.             new AlertDialog.Builder(this)
  142.             .setTitle("Lengkapi Data")
  143.             .setMessage("Silakan lengkapi data "+item)
  144.             .setNeutralButton("OK", new DialogInterface.OnClickListener() {
  145.                 public void onClick(DialogInterface dlg, int sumthin) {
  146.                 }})
  147.             .show();
  148.         }
  149.  
  150.       public void sukses(String item,String ex){
  151.             new AlertDialog.Builder(this)
  152.             .setTitle("Sukses "+ex)
  153.             .setMessage(ex+" data "+item+" Berhasil")
  154.             .setNeutralButton("OK", new DialogInterface.OnClickListener() {
  155.                 public void onClick(DialogInterface dlg, int sumthin) {
  156.                     Intent i = new Intent(Login.this,Menu_pengusaha.class);
  157.                     i.putExtra("xmlbio", xml);
  158.                     startActivity(i);
  159.                      // finish();
  160.                 }})
  161.             .show();
  162.         }
  163.  
  164.       public void gagal(String item){
  165.             new AlertDialog.Builder(this)
  166.             .setTitle("Gagal Login")
  167.             .setMessage("Login "+item+" ,, Gagal")
  168.             .setNeutralButton("OK", new DialogInterface.OnClickListener() {
  169.                 public void onClick(DialogInterface dlg, int sumthin) {
  170.                 }})
  171.             .show();
  172.         }
  173.  
  174.  
  175.       public boolean onKeyDown(int keyCode, KeyEvent event) {
  176.             if (keyCode == KeyEvent.KEYCODE_BACK) {
  177.                 finish();
  178.                     return true;
  179.             }
  180.         return super.onKeyDown(keyCode, event);
  181.     }  
  182.        
  183.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement