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
ProgressDialog progressDialog; Activity activity; AlertDialog.Builder builder;
this.context = context; activity = (Activity) context; }
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(); }
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];
URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
bufferedWriter.flush(); bufferedWriter.close(); outputStream.close();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); String line = "";
stringBuilder.append(line + "\n"); }
Thread.sleep(10000); return stringBuilder.toString().trim();
e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } return null; }
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.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;
Button btn_login; TextView yuk; AlertDialog.Builder builder;
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome);
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()); } } });
} |