How to prevent app exit following Application Error The connection to the server was unsuccessful?

  • 1
  • Problem
  • Updated 7 years ago
Hi,

I have an app that makes an ajax request to an endpoint that might not exist. If the endpoint does not exist, then a dialog appears with the following message:
Application Error The connection to the server was unsuccessful. (file:///android_asset/www/index.html)

Once I hit the OK button the app is shutdown.

I noticed the following logs in the ddms:
CordovaWebView: TIMEOUT ERROR!

Cordova: CordovaWebViewClient.onReceivedError: Error code=-6 Description=The connection to the server was unsuccessful. URL=file:///android_asset/www/index.html

DroidGap: onMessage(onReceivedError,{"errorCode":-6,"url":"file:\/\/\/android_asset\/www\/index.html","description":"The connection to the server was unsuccessful."})

I have read many posts about this error, but most people are happy just adjusting the timeout of their ajax call. This is not an option for me because it is not a timeout issue. It is perfectly normal for the endpoint to not exist in some cases in my app. I want my app to handle the fact that the endpoint did not exist rather than the framework handling it and shutting down my app. I want to simply handle it in the failure function of the ajax call. The failure function does get called, but there does not seem to be anyway of preventing the app from being shutdown.

Any help on this issue would be greatly appreciated.

Sincerely,

dustfinger.
Photo of dustfinger

dustfinger

  • 2 Posts
  • 0 Reply Likes

Posted 8 years ago

  • 1
Photo of Amir

Amir

  • 8261 Posts
  • 263 Reply Likes
Hi Dustfinger

You could try ask ChildBrowser to do the checking instead of your main index.html. Let us know if that didn't help you.

Thanks
-Amir
Photo of dustfinger

dustfinger

  • 2 Posts
  • 0 Reply Likes
Hi Amir,

Thank you for your quick reply. I appreciate it. Unfortunately the ChildBrowser plugin will not resolve my issue because I am not trying to open a page. I am calling endpoints using ajax with the purpose of invoking operations on the server side. Sometimes JSON will be returned, but sometimes the endpoints will be void. A page will never be opened though and ChildBrowser seems to be all about opening pages in a new browser window.

In the event that a URI is unreachable, I would like my app to handle that case in its own way, rather than letting phonegap shut my app down. Any thoughts on how this might be accomplished? Perhaps there is away of intercepting the event that causes phonegap to shutdown the app and prevent it from bubbling up, but so far I have not found away of doing this.

Sincerely,

dustfinger.
Photo of Anis Kadri

Anis Kadri, Employee

  • 1 Post
  • 0 Reply Likes
Hi dustfinger,

That error (a timeout error) happens when you try to load an unreachable host into the WebView. What you can do is check if the host is reachable first (see below) before trying to load it.

As simple/dumb as it seems, the following code does not show the dialog for me and just prints the content if the host is reachable or an error message if the host can't be reached. Give it a try ?



var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://www!@.someunreachablehost.com', false); // try with other unreachable hosts
xhr.send();
if(xhr.status == 200) {
console.log(xhr.responseText); // do whatever here
} else {
console.log("ERROR YO");
}



You could test if your end point is reachable that way and then load it if it is. Otherwise you just handle the error.

Tested with Cordova for Android version 2.0.0

Let me know if it works for you.

Anis
Photo of emagma

emagma

  • 1 Post
  • 0 Reply Likes
Hi Anis,
Thanks for the tip, will be very usefull in my case
Cheers
Loïc