【问题标题】:Android WebView and Word OnlineAndroid WebView 和 Word Online
【发布时间】:2019-08-09 10:19:36
【问题描述】:

我正在开发一个 Android 项目,我必须在其中打开一个 Word Online 文档,该文档必须在不暴露 URL 的情况下打开。我有以下代码可以打开一个文档,但是发生了某种错误。我很确定这与权限有关,但我不确定我到底缺少什么。

FrameLayout.LayoutParams rl = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
                android.webkit.WebView wView = new android.webkit.WebView(this.getContext());
                wView.setId(0x100);
                //wView.setWebChromeClient(new WebChromeClient());
                wView.setWebViewClient(new WebViewClient());
                wView.getSettings().setJavaScriptEnabled(true);
                wView.getSettings().setAllowFileAccess(true);
                wView.getSettings().setAllowUniversalAccessFromFileURLs(true);
                wView.getSettings().setAllowContentAccess(true);
                wView.getSettings().setAppCacheEnabled(true);
                wView.getSettings().setAppCachePath(getContext().getCacheDir().getPath());

                wView.setScrollContainer(false);
                wView.setLayoutParams(rl);
                wView.loadUrl(docURL);

这是显示视图时的日志

I/WebViewFactory: Loading com.android.chrome version 72.0.3626.121 (code 362612102)
I/cr_LibraryLoader: Time to load native libraries: 17 ms (timestamps 4146-4163)
I/chromium: [INFO:library_loader_hooks.cc(42)] Chromium logging enabled: level = 0, default verbosity = 0
I/cr_LibraryLoader: Expected native library version number "72.0.3626.121", actual native library version number "72.0.3626.121"
W/cr_ChildProcLH: Create a new ChildConnectionAllocator with package name = com.android.chrome, sandboxed = true
I/cr_BrowserStartup: Initializing chromium process, singleProcess=false
W/ResourceType: Failure getting entry for 0x7f120538 (t=17 e=1336) (error -2147483647)
W/nicle.chronicle: type=1400 audit(0.0:15466): avc: denied { read } for name="vmstat" dev="proc" ino=4026532345 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:proc:s0 tclass=file permissive=0
W/Adreno-EGL: <qeglDrvAPI_eglGetConfigAttrib:612>: EGL_BAD_ATTRIBUTE
W/Chrome_InProcGp: type=1400 audit(0.0:15467): avc: denied { search } for name="kgsl-3d0" dev="sysfs" ino=19452 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:sysfs_kgsl:s0 tclass=dir permissive=0
W/Adreno-ES20: <get_gpu_clk:229>: open failed: errno 13
W/Adreno-EGL: <qeglDrvAPI_eglGetConfigAttrib:612>: EGL_BAD_ATTRIBUTE
I/Choreographer: Skipped 46 frames!  The application may be doing too much work on its main thread.
W/cr_CrashFileManager: /data/user/0/com.gochronicle.chronicle/cache/WebView/Crash Reports does not exist or is not a directory
W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
Unrecognized profile 2130706434 for video/avc
W/VideoCapabilities: Unsupported mime video/divx
W/VideoCapabilities: Unsupported mime video/divx4
W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
Unrecognized profile 2130706434 for video/avc
I/VideoCapabilities: Unsupported profile 4 for video/mp4v-es
W/Adreno-ES20: <get_gpu_clk:229>: open failed: errno 13
W/Chrome_InProcGp: type=1400 audit(0.0:15468): avc: denied { search } for name="kgsl-3d0" dev="sysfs" ino=19452 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:sysfs_kgsl:s0 tclass=dir permissive=0
 I/zygote: Do partial code cache collection, code=1006KB, data=690KB
I/zygote: After code cache collection, code=1006KB, data=690KB
Increasing code cache capacity to 3MB
I/chromium: [INFO:CONSOLE(1)] "Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.", source: https://c1-word-view-15.cdn.office.net/wv/s/161151337776_App_Scripts/WordViewer.js (1)
I/chromium: [INFO:CONSOLE(0)] "The resource https://c1-word-edit-15.cdn.office.net/we/s/161151337776_App_Scripts/jSanity.js was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it has an appropriate `as` value and it is preloaded intentionally.", source:  (0)
I/chromium: [INFO:CONSOLE(0)] "The resource https://c1-word-edit-15.cdn.office.net/we/s/161151337776_App_Scripts/Compat.js was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it has an appropriate `as` value and it is preloaded intentionally.", source:  (0)
I/chromium: [INFO:CONSOLE(0)] "The resource https://c1-officeapps-15.cdn.office.net/we/s/161151337776_App_Scripts/MicrosoftAjax.js was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it has an appropriate `as` value and it is preloaded intentionally.", source:  (0)
[INFO:CONSOLE(0)] "The resource https://c1-word-edit-15.cdn.office.net/we/s/161151337776_App_Scripts/WordEditor.js was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it has an appropriate `as` value and it is preloaded intentionally.", source:  (0)
[INFO:CONSOLE(0)] "The resource https://c1-word-edit-15.cdn.office.net/we/s/161151337776_App_Scripts/wacbootwe.js was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it has an appropriate `as` value and it is preloaded intentionally.", source:  (0)

这似乎是错误,尽管我已经完成了所有 webview 设置并授予了非常广泛的权限

D/libc-netbsd: getaddrinfo: word-edit.officeapps.live.com get result from proxy gai_error = 0
D/OpenSSLLib: OpensslErr:Module:12(116:176); file:external/boringssl/src/crypto/asn1/asn1_lib.c ;Line:186;Function:ASN1_get_object
D/OpenSSLLib: OpensslErr:Module:12(116:176); file:external/boringssl/src/crypto/asn1/asn1_lib.c ;Line:186;Function:ASN1_get_object
D/OpenSSLLib: OpensslErr:Module:12(116:176); file:external/boringssl/src/crypto/asn1/asn1_lib.c ;Line:186;Function:ASN1_get_object
OpensslErr:Module:12(116:176); file:external/boringssl/src/crypto/asn1/asn1_lib.c ;Line:186;Function:ASN1_get_object
D/libc-netbsd: [getaddrinfo]: hostname=word-view.officeapps.live.com; servname=(null); netid=0; mark=0
[getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=1024; ai_family=0
D/libc-netbsd: getaddrinfo: word-view.officeapps.live.com get result from proxy gai_error = 0
D/libc-netbsd: [getaddrinfo]: hostname=c1-officeapps-15.cdn.office.net; servname=(null); netid=0; mark=0
[getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=1024; ai_family=0
D/libc-netbsd: [getaddrinfo]: hostname=c1-word-view-15.cdn.office.net; servname=(null); netid=0; mark=0
[getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=1024; ai_family=0
D/libc-netbsd: getaddrinfo: c1-word-view-15.cdn.office.net get result from proxy gai_error = 0
D/libc-netbsd: getaddrinfo: c1-officeapps-15.cdn.office.net get result from proxy gai_error = 0
D/OpenSSLLib: OpensslErr:Module:12(116:176); file:external/boringssl/src/crypto/asn1/asn1_lib.c ;Line:186;Function:ASN1_get_object
OpensslErr:Module:12(116:176); file:external/boringssl/src/crypto/asn1/asn1_lib.c ;Line:186;Function:ASN1_get_object

【问题讨论】:

  • 您没有错误。这些是信息和警告日志消息。
  • @Onik 我从今天的测试中添加了额外的日志记录。有趣的是,当我在模拟器中使用 Android 9 运行它时,它可以工作。但是当我使用 8 或 6 时,我拥有的两个物理设备(都是平板电脑),我得到了上面的错误。
  • 在真实设备上,你让 WebView 工作了吗?如果没有,屏幕上到底发生了什么?
  • @Onik 我在网站尝试加载后添加了错误消息的屏幕截图
  • 您介意发帖docURL 以便有人可以重现该问题吗?

标签: java android webview


【解决方案1】:

您可以使用WebView 和谷歌文档(如下所示)通过另一种方式实现此目的。

String url = "http://docs.google.com/gview?embedded=true&url="+YOUR_DOC_URL;

private void loadWebViewAndDocUrl()
{
    String googleDocViewer = "<iframe src=\""+url+"\" style=\"width:100%; height:100%;\" frameborder=\"0\" allowtransparency=\"true\"></iframe>";
    progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
    final WebView wvExternalLink = (WebView) view.findViewById(R.id.wvExternalLink);
    wvExternalLink.getSettings().setJavaScriptEnabled(true);

    wvExternalLink.loadData(googleDocViewer, "text/html", "utf-8");

    wvExternalLink.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            wvExternalLink.loadUrl(url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            // Show progress till url loads completely using progressbar
        }
    });


}

【讨论】:

    【解决方案2】:

    你可以试试

    WebView urlWebView = (WebView)findViewById(R.id.containWebView);
    urlWebView.setWebViewClient(new AppWebViewClients());
    urlWebView.getSettings().setJavaScriptEnabled(true);
    urlWebView.getSettings().setUseWideViewPort(true);
    urlWebView.loadUrl("http://docs.google.com/gview?embedded=true&url="
                    + "YOUR_DOC_URL_HERE"); 
    
    public class AppWebViewClients extends WebViewClient {
    
    
    
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
    
        }
    }
    

    【讨论】:

      【解决方案3】:

      您应该尝试在平板电脑的浏览器(最好是 Chrome)上打开您的docURL

      您的代码适用于我的 this 文档,适用于三星 Galaxy J7 Pro、Android 8.1 和 Moto G4、Android 7.0。两个物理设备。它没有显示 URL。

      它也适用于模拟 Android Nexus 5 API 23,即 Android 6.0。

      您需要的唯一权限是:

      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;

      确保在您的平板电脑上更新Android WebView

      这也可能是您的 DNS 设置、文档内容或文档域的问题。我为我的测试创建了链接,转到 File -> Share -> Insert Into Blog or or or Site,并为生成的 iframe 获取 src 属性的内容。

      【讨论】:

        【解决方案4】:

        在您的日志文件中,我可以看到导致崩溃的两个主要原因。

        W/cr_CrashFileManager: 
        /data/user/0/com.gochronicle.chronicle/cache/WebView/Crash Reports does not 
        exist or is not a directory
        
        
        I/Choreographer: Skipped 46 frames!  The application may be doing too much 
        work on its main thread.
        

        前一个错误可能是由于appCachePath路径错误导致处理耗时。

        后面的错误是由于您的代码/webview 需要很长时间来处理并且因此而跳过帧,这可能是因为您在应用程序的核心进行了一些繁重的处理,导致线程停止一段时间。

        解决方案是使用与主 UI 线程分开的线程,您可以在 android 中使用 AsyncTask。

        在您的情况下,如果错误是由于找不到 appCachePath 的目录造成的,则可能不需要异步任务。

        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        FrameLayout.LayoutParams rl = new 
        FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 
        FrameLayout.LayoutParams.MATCH_PARENT);
                    android.webkit.WebView wView = new 
        android.webkit.WebView(this.getContext());
                    wView.setId(0x100);
                    //wView.setWebChromeClient(new WebChromeClient());
                    wView.setWebViewClient(new WebViewClient());
                    wView.getSettings().setJavaScriptEnabled(true);
                    wView.getSettings().setAllowFileAccess(true);
                    wView.getSettings().setAllowUniversalAccessFromFileURLs(true);
                    wView.getSettings().setAllowContentAccess(true);
                    wView.getSettings().setAppCacheEnabled(true);
                    //also check this line of code for appCachePath
                    String appCachePath = this.getCacheDir().getAbsolutePath();
                     webSettings.setAppCachePath(appCachePath);
        
        
        
                    wView.setScrollContainer(false);
                    wView.setLayoutParams(rl);
        
                    //execute asynctTask
                  new LoadWebViewAsync.execute();
        

        }

           class LoadWebViewAsync extends AsyncTask<Void, Void, Void> {
        
                @Override
                protected Void doInBackground(Void... params) {
        
                  wView.loadUrl(linktourl);
                    return null;
                }
            };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-12
          相关资源
          最近更新 更多