Membuat login android dengan php mysql

Setelah kita membuat tampilan untuk login sekarang kita akan langsung ke proses selanjutnya.

public class proses_login extends AsyncTask {


    private String url_proses_login = "//ip-kalian/proses_android/proses_login.php"; // link proses login di php


    Context context;

    ProgressDialog progressDialog;

    Activity activity;

    AlertDialog.Builder builder;


    public proses_crud[Context context] {

        this.context = context;

        activity = [Activity] context;

    }


    @Override

    protected void onPreExecute[] {

        super.onPreExecute[];

        builder = new AlertDialog.Builder[activity];

        progressDialog = new ProgressDialog[context];

        progressDialog.setMessage["Koneksi ke server..."];

        progressDialog.setIndeterminate[true];

        progressDialog.setCancelable[false];

        progressDialog.show[];

    }


    @Override

    protected String doInBackground[String... params] {

        String method = params[0];

        if [method.equals["Login"]]{

            try {

                URL url = new URL[url_proses_login];

                HttpURLConnection connection = [HttpURLConnection] url.openConnection[];

                connection.setRequestMethod["POST"];

                connection.setDoOutput[true];

                connection.setDoInput[true];

                OutputStream outputStream = connection.getOutputStream[];

                BufferedWriter bufferedWriter = new BufferedWriter[new OutputStreamWriter[outputStream, "UTF-8"]];

                String username, password;

                username = params[1];

                password = params[2];


                String data_login = URLEncoder.encode["username", "UTF-8"] + "=" + URLEncoder.encode[username, "UTF-8"] + "&" +

                        URLEncoder.encode["password", "UTF-8"] + "=" + URLEncoder.encode[password, "UTF-8"];


                bufferedWriter.write[data_login];

                bufferedWriter.flush[];

                bufferedWriter.close[];

                outputStream.close[];


                InputStream inputStream = connection.getInputStream[];

                BufferedReader bufferedReader = new BufferedReader[new InputStreamReader[inputStream]];

                StringBuilder stringBuilder = new StringBuilder[];

                String line = "";


                while [[line = bufferedReader.readLine[]] != null]{

                    stringBuilder.append[line + "\n"];

                }


                connection.disconnect[];

                Thread.sleep[10000];

                return stringBuilder.toString[].trim[];


            } catch [MalformedURLException e] {

                e.printStackTrace[];

            } catch [IOException e] {

                e.printStackTrace[];

            } catch [InterruptedException e] {

                e.printStackTrace[];

            }

        }

        return null;

    }


    @Override

    protected void onPostExecute[String s] {

        super.onPostExecute[s];

        try {

            JSONObject jsonObject = new JSONObject[s];

            JSONArray jsonArray = jsonObject.getJSONArray["Server"];

            JSONObject JO = jsonArray.getJSONObject[0];

            String code = JO.getString["code"];

            String message = JO.getString["pesan"];

            if [code.equals["login_true"]]{

                builder.setMessage[message];

                builder.setPositiveButton["OK", new DialogInterface.OnClickListener[] {

                    @Override

                    public void onClick[DialogInterface dialogInterface, int i] {

                        progressDialog.dismiss[];

                        context.startActivity[new Intent[context, HomeActivity.class]];

                    }

                }];

                AlertDialog alertDialog = builder.create[];

                alertDialog.show[];

            }else if [code.equals["login_false"]]{

                builder.setMessage[message];

                builder.setPositiveButton["OK", new DialogInterface.OnClickListener[] {

                    @Override

                    public void onClick[DialogInterface dialogInterface, int i] {

                        progressDialog.dismiss[];

                    }

                }];

                AlertDialog alertDialog = builder.create[];

                alertDialog.show[];

            }

        } catch [JSONException e] {

            e.printStackTrace[];

        }


    }

}

package com.example.lenovo.myapplication;


import android.app.AlertDialog;

import android.content.DialogInterface;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;


import com.example.lenovo.myapplication.proses.proses_crud;


public class WelcomeActivity extends AppCompatActivity {


    EditText username, password;

    Button btn_login;

    TextView yuk;

    AlertDialog.Builder builder;


    @Override

    protected void onCreate[Bundle savedInstanceState] {

        super.onCreate[savedInstanceState];

        setContentView[R.layout.activity_welcome];


        username = [EditText] findViewById[R.id.editText];

        password = [EditText] findViewById[R.id.editText2];

        btn_login = [Button] findViewById[R.id.button];

        yuk = [TextView] findViewById[R.id.yuk_daftar];

        yuk.setOnClickListener[new View.OnClickListener[] {

            @Override

            public void onClick[View v] {

                Intent intent = new Intent[WelcomeActivity.this, FormRegister.class];

                startActivity[intent];

            }

        }];

        btn_login.setOnClickListener[new View.OnClickListener[] {

            @Override

            public void onClick[View v] {

                if [username.getText[].toString[].equals[""]||password.getText[].toString[].equals[""]]{

                    builder = new AlertDialog.Builder[WelcomeActivity.this];

                    builder.setTitle["Warning..!"];

                    builder.setMessage["Your fill not input..!!"];

                    builder.setPositiveButton["OK", new DialogInterface.OnClickListener[] {

                        @Override

                        public void onClick[DialogInterface dialogInterface, int i] {

                            dialogInterface.dismiss[];

                        }

                    }];

                    AlertDialog alertDialog = builder.create[];

                    alertDialog.show[];

                }

                else {

                    proses_crud backgroundTask = new proses_crud[WelcomeActivity.this];

                    backgroundTask.execute["Login", username.getText[].toString[],password.getText[].toString[]];

                }

            }

        }];


    }

}

Bài mới nhất

Chủ Đề