Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class proses_registrasi extends AsyncTask<String,Void, String> {
- private String url_proses_daftar = "http://ip kalian/proses_android/proses_daftar.php"; // link proses daftar di php
- Context context;
- ProgressDialog progressDialog;
- Activity activity;
- AlertDialog.Builder builder;
- public proses_registrasi(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("Daftar")){
- try {
- URL url = new URL(url_proses_daftar);
- 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_daftar = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
- URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
- bufferedWriter.write(data_daftar);
- 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("daftar_true")){
- 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();
- }else if (code.equals("daftar_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();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment