我在使用onPageFinished方法在頁面上正確運行一些JS時遇到了一些問題。在Android WebView中運行Javascript - onPageFinished循環
下面的代碼包含在我創建的類中,它擴展了AsyncTask來獲取和解析其他地方保存的JSON文件。
我能夠正確獲取JSON文件,解析數據並獲取並設置WebView的URL。一切工作負載,因爲它應該直到我試圖運行一些JS onPageFinished方法。
//onPostExecute method runs when the doInBackground method is completed
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
//Casting as WebView as findViewById doesnt explicity return a value type.
webView = (WebView) findViewById(R.id.webView);
//Obtaining the websettings of the webView
WebSettings webViewSettings = webView.getSettings();
//Setting Javascript enabled
webViewSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new webViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView.loadUrl("document.getElementById('field_133').value = 'Test';");
Log.d("onPageFinished", "The Page has finished loading");
}
});
//Obtaining the first item in the cellRef List Array - From here we will access the Url data for the train operator.
parsedUrl = cellRef.get(0).getUrl();
//load the page we parsed from online file
webView.loadUrl(parsedUrl);
Log.d("loadUrl", "Now load the parsed Url");
}
所有我期待在此刻要做的就是測試的JS能夠正確填充一個文本框一旦頁面加載了「測試」的價值 - 但是,WebView中似乎停留在循環裝載&試圖運行時刷新(看到的「頁面加載完成」重複打印logcat中):
webView.loadUrl("document.getElementById('field_133').value = 'Test';");
這是試圖一些JS注入在Android中的WebView正確的方法是什麼?道歉,如果有明顯的缺失,我的經驗大部分在於斯威夫特。
任何幫助,將不勝感激。
謝謝
這看起來已經停止了「循環」,但現在我在正確的網址短暫(約0.5秒)所示的頁面,那麼它顯示一個白色的屏幕,「測試'在WebView的左上角。是否有一種休息條款,您需要以與您脫離開關相同的方式加入? – 2015-04-01 15:57:25