Untitled

 avatar
unknown
plain_text
a year ago
3.5 kB
5
Indexable
package org.traccar.client;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity2 extends AppCompatActivity {

    private EditText editText1, editText2, editText3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        editText1 = findViewById(R.id.editText1);
        editText2 = findViewById(R.id.editText2);
        editText3 = findViewById(R.id.editText3);

        Button btnSubmit = findViewById(R.id.btnSubmit);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Validate data in textboxes
                if (validateData()) {
                    // Show progress dialog
                    showProgressDialog();

                    // Call API using ExecutorService
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.submit(new APICallTask());

                    // Note: In a real-world scenario, you may want to manage the executor lifecycle appropriately
                }
            }
        });
    }

    private boolean validateData() {
        // Same validation logic as before
        // ...

        return true;
    }

    private void showProgressDialog() {
        ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Calling API...");
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    private void dismissProgressDialog() {
        ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.dismiss();
    }

    private class APICallTask implements Runnable {

        @Override
        public void run() {
            // Replace the URL with your API endpoint
            String apiUrl = "https://your.api.endpoint";

            try {
                URL url = new URL(apiUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", "application/json");

                // Replace with your API request body
                // ...

                // Same API call logic as before
                // ...

                int responseCode = connection.getResponseCode();

                if (responseCode == HttpURLConnection.HTTP_OK) {
                    // Handle API response
                    // ...
                } else {
                    // Handle API error responses
                    // ...
                }
            } catch (IOException e) {
                e.printStackTrace();
                // Handle exceptions
                // ...
            } finally {
                // Dismiss progress dialog on UI thread
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
                    }
                });
            }
        }
    }
}
Editor is loading...
Leave a Comment