Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.5 kB
2
Indexable
Never
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import com.google.gson.JsonObject;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private EditText editText1, editText2, editText3;

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

        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 OkHttp
                    OkHttpClient client = new OkHttpClient.Builder()
                            .addInterceptor(new okhttp3.logging.HttpLoggingInterceptor().setLevel(okhttp3.logging.HttpLoggingInterceptor.Level.BODY))
                            .build();

                    MediaType JSON = MediaType.parse("application/json; charset=utf-8");
                    String requestBody = "{"
                            + "\"data1\":\"" + editText1.getText().toString().trim() + "\","
                            + "\"data2\":\"" + editText2.getText().toString().trim() + "\","
                            + "\"data3\":\"" + editText3.getText().toString().trim() + "\""
                            + "}";

                    Request request = new Request.Builder()
                            .url("https://your.api.endpoint")
                            .post(RequestBody.create(JSON, requestBody))
                            .build();

                    client.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            dismissProgressDialog();
                            // Handle API call failure
                            // ...
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            dismissProgressDialog();
                            if (response.isSuccessful()) {
                                // Handle successful API response
                                // ...
                            } else {
                                // Handle API error responses
                                // ...
                            }
                        }
                    });
                }
            }
        });
    }

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

        return true;
    }

    private void showProgressDialog() {
        // Code to show progress dialog
        // ...
    }

    private void dismissProgressDialog() {
        // Code to dismiss progress dialog
        // ...
    }
}
Leave a Comment