Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ProsesLogin extends AsyncTask<String,Void,String> {
- //url untuk memanggil link php-nya
- private String login_uri = "Your IP/latihan/Login.php";
- Context etx;
- ProgressDialog progressDialog;
- Activity activity;
- AlertDialog.Builder builder;
- public ProsesLogin(Context etx) {
- this.etx = etx;
- activity = (Activity) etx;
- }
- @Override
- protected void onPreExecute() {
- builder = new AlertDialog.Builder(activity);
- progressDialog = new ProgressDialog(etx);
- progressDialog.setTitle("Please Wait");
- progressDialog.setMessage("Connecting to server...");
- progressDialog.setIndeterminate(true);
- progressDialog.setCancelable(false);
- progressDialog.show();
- }
- @Override
- protected String doInBackground(String... voids) {
- String method = voids[0]; //memanggil String pertama
- if (method.equals("login")) {
- try {
- URL url = new URL(login_uri);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- OutputStream outStream = httpURLConnection.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outStream, "UTF-8"));
- String username, password;
- username = voids[1]; //memanggil String kedua
- password = voids[2]; //memanggil String ketiga
- String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
- URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
- bufferedWriter.write(data);
- bufferedWriter.flush();
- outStream.close();
- bufferedWriter.close();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
- InputStream inputStream = httpURLConnection.getInputStream();
- StringBuilder stringBuilder = new StringBuilder();
- String line = "";
- while ((line = bufferedReader.readLine()) != null) {
- stringBuilder.append(line + "\n");
- }
- httpURLConnection.disconnect();
- Thread.sleep(10000);
- return stringBuilder.toString().trim();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (ProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- // proses untuk mengetahui login itu berhasil atau tidak
- @Override
- protected void onPostExecute(String json) {
- try {
- progressDialog.dismiss();
- JSONObject jsonObject = new JSONObject(json);
- JSONArray jsonArray = jsonObject.getJSONArray("server_response");
- JSONObject JO = jsonArray.getJSONObject(0);
- String code = JO.getString("code");
- String message = JO.getString("message");
- if (code.equals("login_true")) {
- Intent intent = new Intent(activity, HomeActivity.class);
- activity.startActivity(intent);
- } else if (code.equals("login_false")) {
- EditText useroremail, pass;
- useroremail = activity.findViewById(R.id.username);
- pass = activity.findViewById(R.id.password);
- useroremail.setText("");
- pass.setText("");
- Intent intent = new Intent(activity, MainActivity.class);
- activity.startActivity(intent);
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement