Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Connection to the server was unsuccessful" when attempting to call javascript in a WebView
    text
    copied!<p>Unlike other questions of the same title here, the PhoneGap/Android app is working fine and loads views and externally linked javascript files EXCEPT when I'm trying to call a javascript method from te Java side. I'm calling the method like so:</p> <pre><code>public void update(final String data) { appView.post(new Runnable(){ @Override public void run(){ loadUrl("javascript: dispatch('" + data + "')"); } }); } </code></pre> <p>And <code>update</code> is called in a non-UI thread.</p> <p>The thing is that I'm calling the javascript method and the functionality runs, works fine, and does what I expect, but after a few seconds, the app crashes with the error message "Connection to the server was unsuccessful" and the name of the method I'd called. I tried increasing the timeout as suggested in other questions, but it still crashes at the end of the timeout.</p> <p>Incidentally, I also get a warning about a WebView method being called from a non-UI thread, which is why I added the <code>post</code> call, so I'm not sure why that's happening, but it happens right before the timeout error in the log, so maybe it's related?</p> <p><strong>Edit</strong>: </p> <p>I get this error in the log right when the crash happens.</p> <pre><code>03-26 15:21:38.671: W/webview(3159): java.lang.Throwable: Warning: A WebView method was called on thread 'Thread-206'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads. 03-26 15:21:38.671: W/webview(3159): at android.webkit.WebView.checkThread(WebView.java:9468) 03-26 15:21:38.671: W/webview(3159): at android.webkit.WebView.stopLoading(WebView.java:2253) 03-26 15:21:38.671: W/webview(3159): at org.apache.cordova.DroidGap$1$1.run(DroidGap.java:549) 03-26 15:21:38.671: W/webview(3159): at java.lang.Thread.run(Thread.java:856) 03-26 15:21:38.675: E/DroidGap(3159): DroidGap: TIMEOUT ERROR! - calling webViewClient </code></pre> <p>I was told the <code>appView.post</code> would avoid this because it would cause the <code>loadUrl</code> call to happen in the UI thread. Apparently this isn't the case?</p> <p><strong>EDIT</strong>:</p> <p>Problem solved. For the record, I changed the <code>loadURL</code> line to this:</p> <pre><code>sendJavascript("dispatch('" + data + "')"); </code></pre> <p>And it works.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload