2017-06-21 27 views
0

當我的基於Web的應用程序從服務器收到錯誤時,我使用下面的代碼將用戶重定向到錯誤頁面。如何捕捉Android WebView中的iframe錯誤

webview.setWebViewClient(new WebViewClient() { 
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
     webview.loadUrl("file:///android_asset/htmls/error.html"); 
    } 
}); 

但是當在iframe中發生錯誤時(未在網頁本身),在未檢測到錯誤。

p.s .:我對某些操作使用了fancybox,fancybox在某些情況下打開了iframe。

回答

0

也許在您的webview中添加JavascriptInterface?

JavaScriptInterface interface = new JavaScriptInterface(this); 
webview.addJavascriptInterface(JSInterface, "interface"); 

public class JavaScriptInterface { 
    Context mContext; 

    /** Instantiate the interface and set the context */ 
    JavaScriptInterface(Context c) { 
     mContext = c; 
    } 
    @JavascriptInterface 
    public void doSomething() 
    { 
    } 
} 

和JavaScript方面:

<script type="text/javascript"> 
    function execute() 
    { 
    JSInterface.doSomething(); 
    } 
</script> 

<input type="button" value="Click me!" onclick="execute()" /> 
+0

你的意思是我由JavaScript趕上iframe的錯誤,並把它傳遞到Android? –

+0

那麼我怎樣才能設置一個全球監聽器使用JavaScript的任何類型的錯誤? –

+0

是的,我的意思是從腳本中捕獲錯誤,然後將其傳遞給Android JS方法。也許不是最好的解決方案,但我真的不能認爲任何東西,因爲iframe往往是一個問題,尤其是在網絡視圖。 –