简体   繁体   中英

Android WebView and Word Online

I am working on an Android project where I have to open a Word Online document that has to be opened without exposing the URL. I have the following code which opens a document, but some kind of error is happening. I'm pretty sure it has something to do with permissions, but I am unsure of what exactly I am missing.

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);

And here is the log when the view is displayed

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)

Here is what appears to be the error, though I have gone through all of the webview settings and given a very broad range of permissions

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

在此处输入图片说明

You can achieve this by an alternative way using WebView and Google Docs like below.

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
        }
    });


}

You could try

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);

    }
}

You should try to open your docURL on your Tablet's browser, preferably Chrome.

Your code worked for me for this document, on a Samsung Galaxy J7 Pro, Android 8.1, and a Moto G4, Android 7.0. Both physical devices. And it did not show the URL.

It also worked on Simulated Android Nexus 5 API 23 , which is Android 6.0.

The only permission you need is this:

<uses-permission android:name="android.permission.INTERNET" />

Make sure to have the Android WebView up to date on your tablets.

This could be also an issue with your DNS setup, your document's content or the domain for your document. I created the link for my test going to File -> Share -> Insert Into Blog or or Site, and took the content for the src attribute for the generated iframe.

In your log file I can see two main reason that caused the crashes.

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.

The former error could be due to the appCachePath which took time to process due to wrong path.

The later error is due to the fact that your code/webview is taking long to process and frames are being skipped because of it, It maybe because of some heavy processing that you are doing at the heart of your application which causes the thread to stop for a while.

The solution would be to use thread separate from main UI thread for which you can use AsyncTask in android.

In your case Async task might not be required if the error was due to directory not found for 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;
        }
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM