首页 > 解决方案 > 即使启用 javascript,Android WebView 也不会加载 javascript 文件或函数

问题描述

我正在尝试创建一个要访问网站的 webview 应用程序。该网站在 chrome 上运行良好,但是当我运行 webview 应用程序时......它没有在日志中加载 javascript 文件/函数。我收到“未捕获的错误,未定义函数...使用 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();
        }
    }*/
}

我已经在 stackoverflow 上搜索了答案,但没有一个能解决我的问题。任何帮助表示赞赏。

我已经在 stackoverflow 上搜索了答案,但没有一个能解决我的问题。

这是登录页面后的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)

出现上述错误。

标签: androidwebviewapkmobile-website

解决方案


我在 webview 和 chrome 中测试了你的 url。在 chrome 中,当我在整页中加载您的 Url 时,它 在滚动条上显示了一个设置图标。但是当我像移动屏幕一样压缩 chrome 标签时,设置图标消失了。所以我认为它的网站或网址问题。在 android web 视图中显示为相同的视图和功能,如压缩的 chrome 屏幕大小。

谢谢


推荐阅读