Advertisement
Guest User

ProsesLogin

a guest
Feb 8th, 2018
2,464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. public class ProsesLogin extends AsyncTask<String,Void,String> {
  2.     //url untuk memanggil link php-nya    
  3.     private String login_uri = "Your IP/latihan/Login.php";
  4.     Context etx;
  5.     ProgressDialog progressDialog;
  6.     Activity activity;
  7.     AlertDialog.Builder builder;
  8.  
  9.     public ProsesLogin(Context etx) {
  10.         this.etx = etx;
  11.         activity = (Activity) etx;
  12.     }
  13.  
  14.     @Override
  15.     protected void onPreExecute() {
  16.         builder = new AlertDialog.Builder(activity);
  17.         progressDialog = new ProgressDialog(etx);
  18.         progressDialog.setTitle("Please Wait");
  19.         progressDialog.setMessage("Connecting to server...");
  20.         progressDialog.setIndeterminate(true);
  21.         progressDialog.setCancelable(false);
  22.         progressDialog.show();
  23.     }
  24.  
  25.     @Override
  26.     protected String doInBackground(String... voids) {
  27.         String method = voids[0]; //memanggil String pertama
  28.         if (method.equals("login")) {
  29.             try {
  30.                 URL url = new URL(login_uri);
  31.                 HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  32.                 httpURLConnection.setRequestMethod("POST");
  33.                 httpURLConnection.setDoOutput(true);
  34.                 httpURLConnection.setDoInput(true);
  35.                 OutputStream outStream = httpURLConnection.getOutputStream();
  36.                 BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outStream, "UTF-8"));
  37.                 String username, password;
  38.                 username = voids[1];  //memanggil String kedua
  39.                 password = voids[2];  //memanggil String ketiga
  40.                 String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
  41.                                 URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
  42.                 bufferedWriter.write(data);
  43.                 bufferedWriter.flush();
  44.                 outStream.close();
  45.                 bufferedWriter.close();
  46.                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  47.                 InputStream inputStream = httpURLConnection.getInputStream();
  48.                 StringBuilder stringBuilder = new StringBuilder();
  49.                 String line = "";
  50.                 while ((line = bufferedReader.readLine()) != null) {
  51.                     stringBuilder.append(line + "\n");
  52.                 }
  53.                 httpURLConnection.disconnect();
  54.                 Thread.sleep(10000);
  55.                 return stringBuilder.toString().trim();
  56.             } catch (MalformedURLException e) {
  57.                 e.printStackTrace();
  58.             } catch (UnsupportedEncodingException e) {
  59.                 e.printStackTrace();
  60.             } catch (ProtocolException e) {
  61.                 e.printStackTrace();
  62.             } catch (IOException e) {
  63.                 e.printStackTrace();
  64.             } catch (InterruptedException e) {
  65.                 e.printStackTrace();
  66.             }
  67.         }
  68.         return null;
  69.     }
  70.  
  71.     // proses untuk mengetahui login itu berhasil atau tidak
  72.     @Override
  73.     protected void onPostExecute(String json) {
  74.         try {
  75.             progressDialog.dismiss();
  76.             JSONObject jsonObject = new JSONObject(json);
  77.             JSONArray jsonArray = jsonObject.getJSONArray("server_response");
  78.             JSONObject JO = jsonArray.getJSONObject(0);
  79.             String code = JO.getString("code");
  80.             String message = JO.getString("message");
  81.             if (code.equals("login_true")) {
  82.                 Intent intent = new Intent(activity, HomeActivity.class);
  83.                 activity.startActivity(intent);
  84.             } else if (code.equals("login_false")) {
  85.                 EditText useroremail, pass;
  86.                 useroremail = activity.findViewById(R.id.username);
  87.                 pass = activity.findViewById(R.id.password);
  88.                 useroremail.setText("");
  89.                 pass.setText("");
  90.                 Intent intent = new Intent(activity, MainActivity.class);
  91.                 activity.startActivity(intent);
  92.             }
  93.         } catch (JSONException e) {
  94.             e.printStackTrace();
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement