Android 即使在启用 javascript 之后,WebView 也未加载 javascript 文件或函数
Android WebView not loading javascript files or functions even after enabling javascript
我正在尝试创建一个我想访问网站的 webview 应用程序。该网站在 chrome 上运行良好,但是当我 运行 webview 应用程序时...它没有在日志中加载 javascript files/function。我收到“未捕获错误,函数未定义...使用 Android Studio 中提到的 js 文件名。
这是我的代码
package com.menux.sss2009;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.os.Bundle;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
CookieManager.getInstance().setAcceptCookie(true);
WebSettings webSettings = webView.getSettings();
webSettings.setAllowContentAccess(true );
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setAppCacheEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDatabaseEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setAppCachePath("/data/data/"+ getPackageName() +"/cache");
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(true);
// webSettings.setJavaScriptEnabled(true);
// webView.getSettings().setPluginState(WebSettings.PluginState.ON);
// webView.getSettings().setAllowFileAccess(true);
// webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
// MyWebChromeClient myWebChromeClient = new MyWebChromeClient();
// webView.setWebChromeClient(myWebChromeClient);
// WebSettings ebSettings = webView.getSettings();
// webSettings.setJavaScriptEnabled(true);
// webSettings.setDomStorageEnabled(true);
// webSettings.setLoadWithOverviewMode(true);
// webSettings.setUseWideViewPort(true);
// webSettings.setBuiltInZoomControls(true);
// webSettings.setDisplayZoomControls(false);
// webSettings.setSupportZoom(true);
webSettings.setDefaultTextEncodingName("utf-8");
// Set cache size to 8 mb by default. should be more than enough
webView.getSettings().setAppCacheMaxSize(1024*1024*64);
// This next one is crazy. It's the DEFAULT location for your app's cache
// But it didn't work for me without this line
// webView.getSettings().setAppCachePath("/data/data/"+ getPackageName() +"/cache");
// webView.getSettings().setAllowFileAccess(true);
// webView.getSettings().setAppCacheEnabled(true);
// webView.getSettings().setDomStorageEnabled(true);
// webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
//
final AppCompatActivity activity = this;
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 100);
}
});
webView.setWebViewClient(new WebViewClient());
/* webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});*/
webView.loadUrl("http://psh2009.net/");
// webView.loadDataWithBaseURL("http://www.psh2009.net/",null,"text/html","UTF-8","http://www.psh2009.net/");
// webView.loadDataWithBaseURL("http://www.psh2009.net/",null,"text/html","UTF-8",null);
}
/*public void onBackPressed() {
if(webView.canGoBack())
{
webView.goBack();
}
else{
super.onBackPressed();
}
}*/
}
我在 Whosebug 上搜索了答案,但 none 解决了我的问题。
感谢任何帮助。
我在 Whosebug 上搜索了答案,但 none 解决了我的问题。
这是登录页面后的 logcat...当主页加载时。
07-25 07:22:49.294 2123-2123/? I/chromium: [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
07-25 07:22:49.314 2123-2123/? I/chromium: [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
07-25 07:22:50.394 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(2)] "Uncaught SyntaxError: Unexpected token in", source: http://117.254.84.212:3000/getjs?nadipdata=%22%7B%22url%22:%22%2Fjs%2Fplugins%2Ficheck%2Ficheck.min.js%22%2C%22referer%22:%22http:%2F%2Fpsh2009.net%2F%22%2C%22host%22:%22psh2009.net%22%2C%22categories%22:%5B0%5D%2C%22reputations%22:%5B1%5D%2C%22nadipdomain%22:1%7D%22&screenheight=480&screenwidth=854&tm=1564019570223&lib=true&fingerprint=c2VwLW5vLXJlZGlyZWN0 (2)
07-25 07:23:11.444 2123-2123/com.menux.psh2009 W/EGL_emulation: eglSurfaceAttrib not implemented 3093 12436
07-25 07:23:22.234 2123-2123/com.menux.psh2009 I/chromium: [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
07-25 07:23:25.624 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(195)] "Uncaught SyntaxError: Unexpected token =", source: http://psh2009.net/js/contentloading/loadingfunctions.js (195)
07-25 07:23:25.714 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(3)] "Uncaught SyntaxError: Unexpected token =", source: http://psh2009.net/js/contentloading/contributionscript.js (3)
07-25 07:23:25.784 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(330)] "Uncaught SyntaxError: Unexpected token =", source: http://psh2009.net/js/contentloading/userscripts.js (330)
07-25 07:23:25.834 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected token =", source: http://psh2009.net/js/contentloading/loansexpenses.js (1)
07-25 07:23:26.514 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(39)] "Uncaught ReferenceError: getchartdata is not defined", source: http://psh2009.net/js/demo_dashboard.js (39)
出现以上错误。
我在网络视图和 chrome 中测试了您的 url。在 chrome 中,当我在整页中加载您的 Url 时,它在滚动条上显示 设置图标 。但是当我像移动屏幕一样压缩 chrome 选项卡时,设置图标 消失了。所以我认为它的网站或 url 问题。在 android 网络视图中显示相同的视图和功能,如压缩 chrome 屏幕尺寸。
谢谢
我无法弄清楚 Nox Player 的问题,但我可以让它在 Vivo 11 和具有 Funtouch OS 4.5 的 Vivo 11 Pro 中工作(基于 Android 8.1) 还有 android 模拟器。最初,当我安装应用程序时,webview 显示错误 'net:ERR_CLEARTEXT_NOT_PERMITTED'。然后我在 'application' 标签的清单文件 'android:usesCleartextTraffic="true"' 中添加了这一行,它没有任何问题地工作。所有 javascript files/functions 都正确加载。感谢大家的意见。
我正在尝试创建一个我想访问网站的 webview 应用程序。该网站在 chrome 上运行良好,但是当我 运行 webview 应用程序时...它没有在日志中加载 javascript files/function。我收到“未捕获错误,函数未定义...使用 Android Studio 中提到的 js 文件名。
这是我的代码
package com.menux.sss2009;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.os.Bundle;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
CookieManager.getInstance().setAcceptCookie(true);
WebSettings webSettings = webView.getSettings();
webSettings.setAllowContentAccess(true );
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setAppCacheEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDatabaseEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setAppCachePath("/data/data/"+ getPackageName() +"/cache");
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(true);
// webSettings.setJavaScriptEnabled(true);
// webView.getSettings().setPluginState(WebSettings.PluginState.ON);
// webView.getSettings().setAllowFileAccess(true);
// webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
// MyWebChromeClient myWebChromeClient = new MyWebChromeClient();
// webView.setWebChromeClient(myWebChromeClient);
// WebSettings ebSettings = webView.getSettings();
// webSettings.setJavaScriptEnabled(true);
// webSettings.setDomStorageEnabled(true);
// webSettings.setLoadWithOverviewMode(true);
// webSettings.setUseWideViewPort(true);
// webSettings.setBuiltInZoomControls(true);
// webSettings.setDisplayZoomControls(false);
// webSettings.setSupportZoom(true);
webSettings.setDefaultTextEncodingName("utf-8");
// Set cache size to 8 mb by default. should be more than enough
webView.getSettings().setAppCacheMaxSize(1024*1024*64);
// This next one is crazy. It's the DEFAULT location for your app's cache
// But it didn't work for me without this line
// webView.getSettings().setAppCachePath("/data/data/"+ getPackageName() +"/cache");
// webView.getSettings().setAllowFileAccess(true);
// webView.getSettings().setAppCacheEnabled(true);
// webView.getSettings().setDomStorageEnabled(true);
// webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
//
final AppCompatActivity activity = this;
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 100);
}
});
webView.setWebViewClient(new WebViewClient());
/* webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});*/
webView.loadUrl("http://psh2009.net/");
// webView.loadDataWithBaseURL("http://www.psh2009.net/",null,"text/html","UTF-8","http://www.psh2009.net/");
// webView.loadDataWithBaseURL("http://www.psh2009.net/",null,"text/html","UTF-8",null);
}
/*public void onBackPressed() {
if(webView.canGoBack())
{
webView.goBack();
}
else{
super.onBackPressed();
}
}*/
}
我在 Whosebug 上搜索了答案,但 none 解决了我的问题。 感谢任何帮助。
我在 Whosebug 上搜索了答案,但 none 解决了我的问题。
这是登录页面后的 logcat...当主页加载时。
07-25 07:22:49.294 2123-2123/? I/chromium: [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
07-25 07:22:49.314 2123-2123/? I/chromium: [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
07-25 07:22:50.394 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(2)] "Uncaught SyntaxError: Unexpected token in", source: http://117.254.84.212:3000/getjs?nadipdata=%22%7B%22url%22:%22%2Fjs%2Fplugins%2Ficheck%2Ficheck.min.js%22%2C%22referer%22:%22http:%2F%2Fpsh2009.net%2F%22%2C%22host%22:%22psh2009.net%22%2C%22categories%22:%5B0%5D%2C%22reputations%22:%5B1%5D%2C%22nadipdomain%22:1%7D%22&screenheight=480&screenwidth=854&tm=1564019570223&lib=true&fingerprint=c2VwLW5vLXJlZGlyZWN0 (2)
07-25 07:23:11.444 2123-2123/com.menux.psh2009 W/EGL_emulation: eglSurfaceAttrib not implemented 3093 12436
07-25 07:23:22.234 2123-2123/com.menux.psh2009 I/chromium: [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
07-25 07:23:25.624 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(195)] "Uncaught SyntaxError: Unexpected token =", source: http://psh2009.net/js/contentloading/loadingfunctions.js (195)
07-25 07:23:25.714 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(3)] "Uncaught SyntaxError: Unexpected token =", source: http://psh2009.net/js/contentloading/contributionscript.js (3)
07-25 07:23:25.784 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(330)] "Uncaught SyntaxError: Unexpected token =", source: http://psh2009.net/js/contentloading/userscripts.js (330)
07-25 07:23:25.834 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected token =", source: http://psh2009.net/js/contentloading/loansexpenses.js (1)
07-25 07:23:26.514 2123-2123/com.menux.psh2009 I/chromium: [INFO:CONSOLE(39)] "Uncaught ReferenceError: getchartdata is not defined", source: http://psh2009.net/js/demo_dashboard.js (39)
出现以上错误。
我在网络视图和 chrome 中测试了您的 url。在 chrome 中,当我在整页中加载您的 Url 时,它在滚动条上显示 设置图标 。但是当我像移动屏幕一样压缩 chrome 选项卡时,设置图标 消失了。所以我认为它的网站或 url 问题。在 android 网络视图中显示相同的视图和功能,如压缩 chrome 屏幕尺寸。
谢谢
我无法弄清楚 Nox Player 的问题,但我可以让它在 Vivo 11 和具有 Funtouch OS 4.5 的 Vivo 11 Pro 中工作(基于 Android 8.1) 还有 android 模拟器。最初,当我安装应用程序时,webview 显示错误 'net:ERR_CLEARTEXT_NOT_PERMITTED'。然后我在 'application' 标签的清单文件 'android:usesCleartextTraffic="true"' 中添加了这一行,它没有任何问题地工作。所有 javascript files/functions 都正确加载。感谢大家的意见。