160

App Dies On Startup (connection to the server was unsuccessful)

I have an Android application that I'm writing using PhoneGap BUILD. The app was working fine earlier, but now it seems I am getting this error after refining my app (some UI changes only)

1) When I start the app I (usually) get:

Application Error - The connection to the server was unsuccessful. (file:///android_asset/www/index.html)

Sorry if this is duplication of any question. I have seen some similar questions here, but i couldn't find a perfect answer or solution. As in my case it was working fine until my last changes.

16 Answers 16

189

In your config.xml file add this line:

<preference name="loadUrlTimeoutValue" value="700000" />
6
  • 6
    You should describe this as well Jul 13, 2015 at 8:02
  • 13
    For anyone wondering: It's the amount of time in milliseconds waited while loading the page before throwing an error: cordova.apache.org/docs/en/latest/config_ref/index.html Mar 28, 2016 at 17:00
  • 13
    <preference name="loadUrlTimeoutValue" value="120000" /> is better in compare with 700000, I mean if your app is not coming up in two minutes, then there is something wrong(and you should fix it by using another solution). Surly you don't want to wait about 11 minutes to see if an app is coming up or not!(that's why we do have timeout concept in the first place, right?:-)) According to the doc, default value is 20000 (20 seconds)
    – SidMorad
    Feb 27, 2017 at 8:17
  • i added this in my config file,but its not working on nexus 5x Mar 3, 2017 at 9:43
  • This is so weird. I had mine set to 70000, and apps were dying after around 30 seconds of trying to load. I changed it to 120000 and now they load successfully in 3-4 seconds. That's all I changed. Argh. Jul 25, 2017 at 14:38
24

Here is the working solution

create a new page main.html

example:

<!doctype html>
<html>
  <head>
   <title>tittle</title>
   <script>
     window.location='./index.html';
   </script>
  </head>
  <body>
  </body>
</html>

change the following in mainactivity.java

super.loadUrl("file:///android_asset/www/index.html");

to

super.loadUrl("file:///android_asset/www/main.html");

Now build your application and it works on any slow connection

refernce.

NOTE: This is a workaround I found in 2013.

8
  • 3
    Actually you just rename your index.html and create main.html with window.location = './index.html'. No need to change super.loadUrl from MainActivity.java
    – M.Salomaa
    Mar 27, 2014 at 6:21
  • 4
    Same effect! But your actual page will be changed to main.html. But I want my actually page to be index.html.BTW, I think your comment is wrong. You need to rename index.html to main.html and create a index.html page, then add window.location = './main.html'. in index.html page. This should work. Mar 27, 2014 at 7:12
  • Yes you right! I miss typed that one :D I meant that create index.html with window.location = './main.html'. :)
    – M.Salomaa
    Mar 27, 2014 at 11:22
  • Why you need your actual page to be index.html?
    – M.Salomaa
    Mar 27, 2014 at 11:24
  • 1
    @madebydavid I answered this 4 years back. At that point, there were no reliable solutions to handle the delay caused when there are a lot of scripts to be loaded. So the app was crashing since the scripts delayed page load. This works because the main.html is light and has no scripts to load, thereby initializing the app faster, and then loading the scripts on index.html. The closing </head> was a typo, added now. Sep 18, 2017 at 14:19
10

Please remove remotely linked jQuery files such as: https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js

Instead, download this file and load it from your local js folder, making the URI:

js/jquery.min.js

6

Remove the external scripts in your index.html

Change this:

<script src="http://code.highcharts.com/highcharts-more.js"></script>

to

<script src="project_folder/highcharts-more.js"></script>
5

I had a similar issue and based on above suggestions I first added "super.setIntegerProperty("loadUrlTimeoutValue", 70000);" but that did not help. So I tried Project -> Clean, that worked and I can launch the app now !

Avinash...

5

Try this,

1.Rename your index.html to “main.html”

2.Create a new “index.html” and put the following content into it:

<!doctype html>
<html>
  <head>
   <title>tittle</title>
   <script>
     window.location='./main.html';
   </script>
  <body>
  </body>
</html>

3.Rebuild your app! No more errors!

4
  • and what should i do if i have to add some arguments like main.html?USER=xxx&SESSION=xxx ? Oct 7, 2015 at 18:21
  • 3
    window.location.href="./main.html?USER=xxx&SESSION=xxx" Oct 9, 2015 at 12:05
  • No cordova and plugins being injected into main.html
    – viskin
    Sep 5, 2016 at 11:46
  • I have added this code but for approx 1 minute, a white screen is coming after splash. What should I do?
    – Anjana
    Aug 31, 2017 at 6:22
5

fixing this on an ionic app, simply add

<preference name="loadUrlTimeoutValue" value="700000" />

to your config.xml file immediately after this line

<platform name="android">
0
4

I had the same on my project.

I tried " super.setIntegerProperty("loadUrlTimeoutValue", 70000); " but to no avail.

I ensured all files were linked properly [ CSS, JS files etc ], validated the HTML using w3c validator [ http://validator.w3.org/#validate_by_upload ] , and cleaned the project [ Project -> Clean ]

It now loads and executes without the same error.

Hope this helps

4

Extending loading Timeout Limit will not solve the problem which caused the error, it just will avoid the system to show the message but performance will be affected whatsoever.

Actual reason: You may be linking files or images to remote locations, and those resources are taking too long to load. (this is likely the most common error)

Definitive solution: move all the scripts, images and css needed to some local folders and load them locally ...

Performance will be increased and error will be effectively solved.

4

Check you index.html file. If you use external resources, that not available when you run application then you can get this error.

In my case I forgot to delete link on debugger script (weinre).

<script src="http://192.168.0.102:8080/target/target-script-min.js#anonymous"></script>

So application worked on emulator because http://192.168.0.102:8080/ was on my localhost and available for emulator.

But when I setup application on mobile phone I had same error, because 192.168.0.102 was not available from mobile network.

2

For my case, the problem was due to losing of the internet connection in my WiFi.

1

In my case I am using ionic and I simply closed the dialog went to apps in the emulator and ran my app from there instead. This worked. I got the idea of that from here since it was just a time out issue.

1

If you are using visual studio. After changing config.xml sometimes you need this

clean build solution rebuild your app

It is working for me.

1

I was facing the same issue. I noticed that in my index i had both the "on device ready" and the "document.ready" function, so removing one of them fixed my problem :)

0

It's quite a niche situation but thought I'd post it here in case it saves somebody else the hours I spent going round in circles.

I reverted to [email protected] after trying out [email protected] and started getting this error when trying to run my app in dev mode.

What I had forgotten is that when I upgraded to 10.0.1 I had to remove the whitelist plugin since it's now part of cordova-android.

Reinstating cordova-plugin-whitelist got everything working again for me!

-8

Another reason this error might occur is: there is no index.html in .../YourApp/www/ !

I just followed the ionic guide, and one of the steps is:

$ rm www/index.html

On iOS this is no problem as during the build the compiler takes some default HTML instead. However, when building for android, NO example index.html is used. Took me sometime to find out ("WHY does it work on iOS, but not on android...?)

Easy solution: create a index.html, save it under .../YourApp/www, rebuild ...et voila!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.