MyBlogShare

Proses Registrasi Android Studio

Jul 7th, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. public class proses_registrasi extends AsyncTask<String,Void, String> {
  2.  
  3.     private String url_proses_daftar = "http://ip kalian/proses_android/proses_daftar.php"; // link proses daftar di php
  4.  
  5.     Context context;
  6.     ProgressDialog progressDialog;
  7.     Activity activity;
  8.     AlertDialog.Builder builder;
  9.  
  10.     public proses_registrasi(Context context) {
  11.         this.context = context;
  12.         activity = (Activity) context;
  13.     }
  14.  
  15.     @Override
  16.     protected void onPreExecute() {
  17.         super.onPreExecute();
  18.         builder = new AlertDialog.Builder(activity);
  19.         progressDialog = new ProgressDialog(context);
  20.         progressDialog.setMessage("Koneksi ke server...");
  21.         progressDialog.setIndeterminate(true);
  22.         progressDialog.setCancelable(false);
  23.         progressDialog.show();
  24.     }
  25.  
  26.     @Override
  27.     protected String doInBackground(String... params) {
  28.         String method = params[0];
  29.         if (method.equals("Daftar")){
  30.             try {
  31.                 URL url = new URL(url_proses_daftar);
  32.                 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  33.                 connection.setRequestMethod("POST");
  34.                 connection.setDoOutput(true);
  35.                 connection.setDoInput(true);
  36.                 OutputStream outputStream = connection.getOutputStream();
  37.                 BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  38.                 String username, password;
  39.                 username = params[1];
  40.                 password = params[2];
  41.  
  42.                 String data_daftar = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
  43.                         URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
  44.  
  45.                 bufferedWriter.write(data_daftar);
  46.                 bufferedWriter.flush();
  47.                 bufferedWriter.close();
  48.                 outputStream.close();
  49.  
  50.                 InputStream inputStream = connection.getInputStream();
  51.                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  52.                 StringBuilder stringBuilder = new StringBuilder();
  53.                 String line = "";
  54.  
  55.                 while ((line = bufferedReader.readLine()) != null){
  56.                     stringBuilder.append(line + "\n");
  57.                 }
  58.  
  59.                 connection.disconnect();
  60.                 Thread.sleep(10000);
  61.                 return stringBuilder.toString().trim();
  62.  
  63.             } catch (MalformedURLException e) {
  64.                 e.printStackTrace();
  65.             } catch (IOException e) {
  66.                 e.printStackTrace();
  67.             } catch (InterruptedException e) {
  68.                 e.printStackTrace();
  69.             }
  70.         }
  71.         return null;
  72.     }
  73.  
  74.     @Override
  75.     protected void onPostExecute(String s) {
  76.         super.onPostExecute(s);
  77.         try {
  78.             JSONObject jsonObject = new JSONObject(s);
  79.             JSONArray jsonArray = jsonObject.getJSONArray("Server");
  80.             JSONObject JO = jsonArray.getJSONObject(0);
  81.             String code = JO.getString("code");
  82.             String message = JO.getString("pesan");
  83.             if (code.equals("daftar_true")){
  84.                 builder.setMessage(message);
  85.                 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  86.                     @Override
  87.                     public void onClick(DialogInterface dialogInterface, int i) {
  88.                         progressDialog.dismiss();
  89.                     }
  90.                 });
  91.                 AlertDialog alertDialog = builder.create();
  92.                 alertDialog.show();
  93.             }else if (code.equals("daftar_false")){
  94.                 builder.setMessage(message);
  95.                 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  96.                     @Override
  97.                     public void onClick(DialogInterface dialogInterface, int i) {
  98.                         progressDialog.dismiss();
  99.                     }
  100.                 });
  101.                 AlertDialog alertDialog = builder.create();
  102.                 alertDialog.show();
  103.             }
  104.         } catch (JSONException e) {
  105.             e.printStackTrace();
  106.         }
  107.  
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment