'Why am i getting this error when trying to communicate with my server?

I am simply trying to implement stripe payments in my app and i am following the their doc here https://stripe.com/docs/payments/quickstart and i keep getting this error ( Failed to connect to localhost/127.0.0.1:4242) , whenever i launch my app. I am not sure why this is happening i already tried adding android:usesCleartextTraffic="true" to my manifest file as some users suggested but to no avail.

public class payment_activity extends AppCompatActivity {

    private String paymentIntentClientSecret;
    private PaymentLauncher paymentLauncher;
    // we need paymentIntentClientSecret to start transaction
    private Button paymentButton;
    CardInputWidget cardInputWidget;

    private PaymentSheet paymentSheet;


    private static final String TAG = "CheckoutActivity";
    private static final String BACKEND_URL = "http://localhost:4242";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment);
        paymentButton = findViewById(R.id.payButton);
        cardInputWidget = findViewById(R.id.cardInputWidget);

        paymentSheet = new PaymentSheet(this, this::onPaymentSheetResult);

        fetchPaymentIntent();


        paymentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PaymentSheet.Configuration configuration = new PaymentSheet.Configuration("Example, Inc.");

                // Present Payment Sheet
                paymentSheet.presentWithPaymentIntent(paymentIntentClientSecret, configuration);
            }
        });


    }




    private void showAlert(String title, @Nullable String message) {
        runOnUiThread(() -> {
            AlertDialog dialog = new AlertDialog.Builder(this)
                    .setTitle(title)
                    .setMessage(message)
                    .setPositiveButton("Ok", null)
                    .create();
            dialog.show();
        });
    }

    private void showToast(String message) {
        runOnUiThread(() -> Toast.makeText(this, message, Toast.LENGTH_LONG).show());
    }

    private void fetchPaymentIntent() {
        final String shoppingCartContent = "{\"items\": [ {\"id\":\"xl-tshirt\"}]}";

        final RequestBody requestBody = RequestBody.create(
                shoppingCartContent,
                MediaType.get("application/json; charset=utf-8")
        );

        Request request = new Request.Builder()
                .url(BACKEND_URL + "/create-payment-intent")
                .post(requestBody)
                .build();

        new OkHttpClient()
                .newCall(request)
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        showAlert("Failed to load data", "Error: " + e.toString());
                    }

                    @Override
                    public void onResponse(
                            @NonNull Call call,
                            @NonNull Response response
                    ) throws IOException {
                        if (!response.isSuccessful()) {
                            showAlert(
                                    "Failed to load page",
                                    "Error: " + response.toString()
                            );
                        } else {
                            final JSONObject responseJson = parseResponse(response.body());
                            paymentIntentClientSecret = responseJson.optString("clientSecret");
                            runOnUiThread(() -> paymentButton.setEnabled(true));
                            Log.i(TAG, "Retrieved PaymentIntent");
                        }
                    }
                });
    }

    private JSONObject parseResponse(ResponseBody responseBody) {
        if (responseBody != null) {
            try {
                return new JSONObject(responseBody.string());
            } catch (IOException | JSONException e) {
                Log.e(TAG, "Error parsing response", e);
            }
        }

        return new JSONObject();
    }

    private void onPaymentSheetResult(
            final PaymentSheetResult paymentSheetResult
    ) {
        if (paymentSheetResult instanceof PaymentSheetResult.Completed) {
            showToast("Payment complete!");
        } else if (paymentSheetResult instanceof PaymentSheetResult.Canceled) {
            Log.i(TAG, "Payment canceled!");
        } else if (paymentSheetResult instanceof PaymentSheetResult.Failed) {
            Throwable error = ((PaymentSheetResult.Failed) paymentSheetResult).getError();
            showAlert("Payment failed", error.getLocalizedMessage());
        }
    }




}


Faild to connect to localhost/127.0.0.1:4242


Solution 1:[1]

I connect to localhost:4242 in my Android apps using the following URL inside Android Studio: http://10.0.2.2:4242

Update your BACKEND_URL constant and give that a try.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RyanM