1

When I press Upload button in my app, the API that is called from string URL executes twice. How can I handle the behaviour correctly so that the API is executed just once on a single click ?

This is the Activity Code:

public class UserLoggedInActivity extends AppCompatActivity {

private static final int SELECT_PICTURE = 100;

Button logout, upload, selectImage;
ImageView imageView;
EditText productName, productPrice;
Uri uri;

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

    logout = findViewById(R.id.logout);
    upload = findViewById(R.id.upload);
    imageView = findViewById(R.id.image);
    productName = findViewById(R.id.productName);
    productPrice = findViewById(R.id.productPrice);
    selectImage = findViewById(R.id.selectImage);

    logout.setOnClickListener(v -> {
        FirebaseAuth.getInstance().signOut();
        Toast.makeText(UserLoggedInActivity.this, "Successfully Logged out", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(UserLoggedInActivity.this, UserLoginActivity.class));
    });

    selectImage.setOnClickListener(v -> {

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
    });

    upload.setOnClickListener(v -> {
        String pName = productName.getText().toString();
        String pPrice = productPrice.getText().toString();
        Log.e("REAL_PATH: ", "The pseudo value is: " + uri.getPath());
        String URL = "http://192.168.1.12:8080/storeProductInfo?file=" + new File("/Users/guppranj/Desktop/download.jpeg")
                + "&&name=" + pName + "&&price=" + pPrice;
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(URL, null,
                response -> Log.e("INFO",response.toString()),
                null);
        requestQueue.add(jsonObjectRequest);
        Toast.makeText(UserLoggedInActivity.this, "Product Info saved successfully", Toast.LENGTH_SHORT).show();
        productName.getText().clear();
        productPrice.getText().clear();
        imageView.setImageResource(android.R.color.transparent);
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    super.onActivityResult(requestCode, resultCode, resultData);
    if (requestCode == SELECT_PICTURE && resultCode == Activity.RESULT_OK) {
        if (resultData != null) {
            uri = resultData.getData();
            imageView.setImageURI(uri);
        }
    }
}

1 Answers1

0

How about you try disable the button after its clicked then perform the upload and once you receive the result back you can enable the same.

upload.setOnClickListener(v -> {
        v.setEnabled(false);
        String pName = productName.getText().toString();
        String pPrice = productPrice.getText().toString();
        Log.e("REAL_PATH: ", "The pseudo value is: " + uri.getPath());
        String URL = "http://192.168.1.12:8080/storeProductInfo?file=" + new File("/Users/guppranj/Desktop/download.jpeg")
                + "&&name=" + pName + "&&price=" + pPrice;
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(URL, null,
                response -> Log.e("INFO",response.toString()),
                null);
        requestQueue.add(jsonObjectRequest);
        Toast.makeText(UserLoggedInActivity.this, "Product Info saved successfully", Toast.LENGTH_SHORT).show();
        productName.getText().clear();
        productPrice.getText().clear();
        imageView.setImageResource(android.R.color.transparent);
        v.setEnabled(true);
    });