10

I build a simple ionic project from this tutorial.

It runs on Xiaomi Mix 2 phone (android version 8.0.0) and on browser without any problem. But when I deployed to the samsung note 2 (android version 4.4.2), it gives an application error with this message: net::ERR_CONNECTION_REFUSED (http://localhost:8080)

Why am I getting this error?

Any advice and suggestions will be appreciated.

Kemal.

chrome
  • 661
  • 2
  • 7
  • 23

7 Answers7

4

I faced the same problem in Android 4.4, Android 6.0 but not in Android 8.0. I just added this code in config.xml to allow the localhost.

<allow-navigation href="http://localhost:8080/*"/>

For more information please follow this link: WKWebView

3

Your problem is caused by the cordova-plugin-ionic-webview plugin that is part of every new or updated Ionic app.

This used to apply only to iOS, where it replaced the UIWebView with WKWebView, but on July 23rd 2018 they released version 2.0 of the plugin, that also included changes to the webview used on Android.

The Android webview now uses a local webserver at localhost:8080 to show your app instead of requesting the files directly from the file system.

Unfortunately this change also included this bit in the documentation:

Requirements
- […]
- Android: Android 5.0+ and cordova-android 6.4+

So cordova-plugin-ionic-webview just doesn’t support Android earlier than 5.0 any more, which of course means your app will not work on Android 4.x.

One solution is to downgrade the plugin to the last version that supported Android 4.x:

ionic cordova plugin add cordova-plugin-ionic-webview@1.2.1

More elaborate information and alternative solutions:
https://ionic.zone/debug/ionic-and-android-4

janpio
  • 10,645
  • 16
  • 64
  • 107
  • https://stackoverflow.com/questions/58317857/failed-to-load-resource-neterr-connection-refused-cordova-android , kindly give a solution to this question – MurugananthamS Oct 11 '19 at 04:27
3

In case anyone is still not able to resolve this error,

  1. Find the compatible webview version which works for you and run the below commands:
    cordova plugin rm cordova-plugin-ionic-webview
    cordova plugin add cordova-plugin-ionic-webview@4.1.3
  1. In case you're using ionic, install the ionic plugin wrapper. (Skip this if your project is not an ionic project)
    npm install @ionic-native/ionic-webview
  1. Finally do clean installation of the platform and plugins. Delete the plugins folder and run the following commands:
    cordova platforms remove android
    ionic cordova build android

Or, if you use ionic,

    ionic cordova platforms remove android
    ionic cordova build android

Additional information: Plugin version cordova-plugin-ionic-webview@4.1.3 worked for me for the below cordova and android versions:

Cordova CLI       : 9.0.0 (cordova-lib@9.0.1)
Cordova Platforms : android 8.1.0
Android target    : android-28
Android SDK Tools : 26.1.1
cordova-android   : 8.1.0
Evan MJ
  • 1,967
  • 13
  • 16
  • 1
    Thanks, to fix I had to upgrade to cordova 10 and webview 5+ as per new play store API level 30 requirements. – Pravin Mar 05 '22 at 16:24
0

You are getting the error because every function and module in Android only works from a certain version on newer. It could be 4.4.3 and up, 5.0 and up, or even 8.0 and up. It just depends. This means if and when you deploy it you will be required to select a minimum version. There is nothing you can do to fix it other than trying on a newer version. You can also change the code completely to work with older modules.

James
  • 1,928
  • 3
  • 13
  • 30
  • 1
    Application's Cordova Platforms android version is 7.0.0 According to this document https://cordova.apache.org/docs/en/latest/guide/platforms/android/index.html , it must support 4.4.2. – chrome Jul 30 '18 at 21:21
  • 2
    After testing, I am getting the same error on all my devices below 5.0. 5.0 works for me. – James Aug 01 '18 at 22:19
  • @chrome, if this answer was correct, please mark it as correct so future readers know this has been solved. – James Aug 03 '18 at 19:33
  • 1
    Unfortunately this answer is not strictly correct - I added an answer with the actual problem and solution. – janpio Aug 25 '18 at 12:57
0

I've had same error even on new devices. After that, I've reinstalled cordova-plugin-ionic-webview plugin. Reinstallation upgraded plugin from 4.0.0 to 5.0.0 and added new line to config.xml:

<allow-navigation href="http://localhost:8100" sessionid="f8f1cc34" />

I'm still waiting for review, but hope this will help.

ZlobnyiSerg
  • 712
  • 1
  • 7
  • 15
0

I had this problem for days and all the solutions I found did not work.

Ionic recommends using Capacitor to build the app and not Cordova. With Cordova does not work, on the other hand with Capacitor it works fine.

In your app directory first delete the Android folder (if any) and the one in Platform / Android folder.

I used these commands in this order launched by Powershell while staying in the APP directory

ionic build

ionic capacitor add android

npx cap open android

The first command will create a www folder. If the last command give an error and does not open Android Studio, do it manually, (I recommend using 4.0 version). Open Android Studio → open folder, navigate to the folder named Android located in the folder in your APP and then it worked for me. In Android Sudio 4 make sure you have Gradle updated and the SDK loaded.

Pietro
  • 127
  • 6
0

/android/CordovaLib/src/org/apache/cordova/engine/SystemWebViewClient.java

  onReceivedError(WebView view, WebResourceRequest request,
                  WebResourceError error) {
    super.onReceivedError(view, request, error);
    if (error.getErrorCode() == WebViewClient.ERROR_CONNECT) {  //net::ERR_CONNECTION_REFUSED

      view.goForward();
      return;
    } else {
      errorMessage();
    }
  }
Fakhar
  • 3,946
  • 39
  • 35