FTP PLUGIN ERROR IN IONIC + ANGULAR WITH CAPACITOR NetworkOnMainThreadException

I need help with the following request when using the IONIC + ANGULAR v6 FTP plugin it throws the following error

‘android.os.NetworkOnMainThreadException’

It does not allow me to upload files to the FTP server.

this.fTP.connect(dominio_server, user, pass).then((res: boolean) =>{    
this.fTP.upload(URI_FILE,URI_DROP_FILE).subscribe(data=>{console.log(data)})
});
{
  "name": "nameApp",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "https://ionicframework.com/",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "^14.0.0",
    "@angular/core": "^14.0.0",
    "@angular/forms": "^14.0.0",
    "@angular/platform-browser": "^14.0.0",
    "@angular/platform-browser-dynamic": "^14.0.0",
    "@angular/router": "^14.0.0",
    "@awesome-cordova-plugins/camera": "^5.44.0",
    "@awesome-cordova-plugins/file": "^5.44.0",
    "@awesome-cordova-plugins/ftp": "^5.44.0",
    "@awesome-cordova-plugins/http": "^5.44.0",
    "@awesome-cordova-plugins/sqlite": "^5.44.0",
    "@awesome-cordova-plugins/video-player": "^5.44.0",
    "@capacitor/android": "4.0.1",
    "@capacitor/app": "4.0.1",
    "@capacitor/core": "4.0.1",
    "@capacitor/haptics": "4.0.1",
    "@capacitor/keyboard": "4.0.1",
    "@capacitor/status-bar": "4.0.1",
    "@ionic/angular": "^6.1.9",
    "cordova-plugin-advanced-http": "^3.3.1",
    "cordova-plugin-camera": "^6.0.0",
    "cordova-plugin-file": "^7.0.0",
    "cordova-plugin-ftp": "^1.2.1",
    "cordova-plugin-video-player": "github:moust/cordova-plugin-videoplayer",
    "cordova-sqlite-storage": "^6.0.0",
    "rxjs": "~6.6.0",
    "swiper": "^8.3.2",
    "tslib": "^2.2.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^14.0.0",
    "@angular-eslint/builder": "~13.0.1",
    "@angular-eslint/eslint-plugin": "~13.0.1",
    "@angular-eslint/eslint-plugin-template": "~13.0.1",
    "@angular-eslint/template-parser": "~13.0.1",
    "@angular/cli": "^14.0.0",
    "@angular/compiler": "^14.0.0",
    "@angular/compiler-cli": "^14.0.0",
    "@angular/language-service": "^14.0.0",
    "@capacitor/cli": "4.0.1",
    "@ionic/angular-toolkit": "^6.0.0",
    "@types/jasmine": "~3.6.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "@typescript-eslint/eslint-plugin": "5.3.0",
    "@typescript-eslint/parser": "5.3.0",
    "eslint": "^7.6.0",
    "eslint-plugin-import": "2.22.1",
    "eslint-plugin-jsdoc": "30.7.6",
    "eslint-plugin-prefer-arrow": "1.2.2",
    "jasmine-core": "~3.8.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~6.3.2",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "typescript": "~4.7.3"
  },
  "description": "An Ionic project"
}

Hello! Unfortunately, there’s not really enough information here to help out much. From a glance, I’d recommend posting an issue on the repo for the FTP plugin, since this is much more likely going to be an issue there than with Capacitor directly. But without a minimal reproduction, that’s about all the help I can offer and suggest.

Hi, I have a really similar issue with ionic 6, capacitor 4 and angular. When I execute the following code (tested in response to a button being pressed or inside ionViewWillEnter) I get the exact same error (‘android.os.NetworkOnMainThreadException’).

inside my sftp service file :
    public async moreTest(): Promise<void> {
        await this.ftp.connect('ftp_host', 'ftp_user', 'ftp_password')
            .then((res: any) => console.log('Login successful', res))
            .catch((error: any) => console.error(error));
        await this.ftp.ls('/wiitime-test');
    }
inside a random page :
    public connectSftp(): void {
        from(this.sftpService.moreTest()).subscribe(() => console.log('yes'));
    }

I don’t know if it’s a bug but I was not able to a way to avoid the exception. It would be really helpful if anybody had a solution or workaround. If you need more detail, I will gladly give them to you.
Have a nice day !

I’ve had same issue, and solved it by adding these two lines of code in capacitor-android package → BridgeActivity class → onCreate function:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

Hopefully it helps.

Hi pekez,
first thank you for you answer!
I ended up doing the same thing in the end (found the same solution yersteday). Sadly, it’s just a temporary workaround since it’s highly recommended that an application respect the strict mode thread policy.
It would be good if we could find the source of this issue in order to help fixing it. If you are also on capacitor 4 there is a really high chance that it’s the ftp plugin fault.
I hope we can find a real fix and have a nice day !

It may be a problem directly from the plugin

What I did to do the file transfer was to create a service to transfer them.

I realized that in Ionic 6 the FTP does not work