java.net.ConnectException: Connection refused

I’m trying to run a basic test on my android application in Android Studio but when I execute the test file, I get the following error:

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Host info: host: 'localhost', ip: '127.0.0.1'
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:559)
at io.appium.java_client.AppiumDriver.startSession(AppiumDriver.java:229)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:157)
at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:80)
at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:92)
at io.appium.java_client.android.AndroidDriver.<init>(AndroidDriver.java:117)
at com.example.dissertationdummyapp.InitialTest.setUp(InitialTest.java:27)
... 32 trimmed
Caused by: org.openqa.selenium.WebDriverException: Connection refused
Build info: version: '4.7.0', revision: '0a5b49d16f'
System info: os.name: 'Linux', os.arch: 'x86_64', os.version: '6.1.23-android14-4-00257-g7e35917775b8-ab9964412', java.version: '0'
Driver info: driver.version: AndroidDriver
at io.appium.java_client.remote.AppiumCommandExecutor.lambda$execute$4(AppiumCommandExecutor.java:274)
at io.appium.java_client.remote.AppiumCommandExecutor$$ExternalSyntheticLambda3.get(Unknown Source:2)
at java.util.Optional.orElseGet(Optional.java:365)
at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:273)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:541)
... 39 more
Caused by: java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:762)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:337)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:776)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:1012)

I see that the error is caused by this error:

java.net.ConnectException: Connection refused

[This is my test case code for reference:]

package com.example.dissertationdummyapp;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebElement;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Objects;

import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;

public class InitialTest {

    public AndroidDriver driver;
    @Before
    public void setUp() throws MalformedURLException {
        UiAutomator2Options options = new UiAutomator2Options()
                .setDeviceName("Pixel_7_API_34")
                .setAppPackage("com.example.dissertationdummyapp")
                .setAppActivity(".MainActivity");

        AndroidDriver driver = new AndroidDriver( new URL("http://127.0.0.1:4723"), options );
    }

    @Test
    public void LoginSuccessTest(){
        System.out.println("Driver: " + driver);
        WebElement username = driver.findElement(AppiumBy.id("com.example.dissertationdummyapp:id/usernameTextField"));
        username.sendKeys("testUsername");
        WebElement password = driver.findElement(AppiumBy.id("com.example.dissertationdummyapp:id/passwordTextField"));
        password.sendKeys("testPassword");
        WebElement submit = driver.findElement(AppiumBy.id("com.example.dissertationdummyapp:id/submitButton"));
        submit.click();
        WebElement msg = driver.findElement(AppiumBy.id("com.example.dissertationdummyapp:id/successMsg"));
        Assert.assertEquals("Welcome testUsername!", msg.getText());
    }

    @Test
    public void SignUpPage(){
        WebElement signupbutton = driver.findElement(AppiumBy.id("com.example.dissertationdummyapp:id/signupbutton"));
        signupbutton.click();
        WebElement appTitle = driver.findElement(AppiumBy.xpath("//android.widget.TextView[@text=\"Sign Up\"]"));
        Assert.assertEquals("Sign Up", appTitle.getText());
    }

    @After
    public void tearDown(){
        if (driver != null){
            driver.quit();
        }
    }
}

I have tried everything from changing the port number to turning off my firewall. Please help