androidstudio:webview加载两次

xuo3flqw  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(852)

我正在做一个小的个人项目,我有一个登录屏幕,输入用户名和密码到一个网页,并点击登录按钮,所有这些都使用javascript。我有一个问题,虽然代码工作,但它执行两次,第一次不工作,第二次工作。
这是我的密码

webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                webView.loadUrl("javascript: {" +
                        "document.getElementById('username').value = '" + username.getText().toString() + "';" +
                        "document.getElementById('password').value = '" + password.getText().toString() + "';" +
                        "document.getElementsByClassName('ZLoginButton DwtButton')[0].click();" +
                        "};"
                );

                System.out.println("================>" + username.getText().toString() + " " + password.getText().toString());
                System.out.println("================> " + webView.getUrl());

                if (webView.getUrl().equals("https://mail.metropolitan.ac.rs/m/zmain")) { // If it goes from login webpage to the main webpage
                    System.out.println("Connected...");
                } else {
                    System.out.println("Disconnected..." + " Website: " + webView.getUrl());
                }
            }
        });

这是输出

I/.example.metap: Waiting for a blocking GC ProfileSaver
W/.example.metap: Accessing hidden method Landroid/media/AudioManager;->getOutputLatency(I)I (greylist, reflection, allowed)
W/cr_media: Requires BLUETOOTH permission
D/HostConnection: HostConnection::get() New Host Connection established 0xcd0965f0, tid 19767
D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2 
E/chromium: [ERROR:gl_surface_egl.cc(549)] eglChooseConfig failed with error EGL_SUCCESS
D/EGL_emulation: eglCreateContext: 0xf6e62210: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0xf6e62210: ver 2 0 (tinfo 0xf71b0490) (first time)
E/chromium: [ERROR:gl_surface_egl.cc(549)] eglChooseConfig failed with error EGL_SUCCESS
I/VideoCapabilities: Unsupported profile 4 for video/mp4v-es
W/cr_MediaCodecUtil: HW encoder for video/avc is not available on this device.
D/EGL_emulation: eglCreateContext: 0xf6e62f30: maj 2 min 0 rcv 2

I/System.out: ================>"myUsername" "myPassword"
I/System.out: ================> https://mail.metropolitan.ac.rs/         
    Disconnected... Website: https://mail.metropolitan.ac.rs/         //<-----------Here is the first

I/chromium: [INFO:CONSOLE(9)] "Error parsing a meta element's content: ';' is not a valid key-value pair separator. Please use ',' instead.", source: https://mail.metropolitan.ac.rs/m/zmain (9)
I/chromium: [INFO:CONSOLE(10)] "An error occured in: https://mail.metropolitan.ac.rs/m/zmain
    {
        msg: Uncaught ReferenceError: dId is not defined,
        line: 1081
    }", source: https://mail.metropolitan.ac.rs/m/zmain (10)
    [INFO:CONSOLE(1081)] "Uncaught ReferenceError: dId is not defined", source: https://mail.metropolitan.ac.rs/m/zmain (1081)

I/System.out: ================>"myUsername" "myPassword"
    ================> https://mail.metropolitan.ac.rs/m/zmain
    Connected...                                                   //<-----------Here is the second

I/chromium: [INFO:CONSOLE(10)] "An error occured in: https://mail.metropolitan.ac.rs/m/zmain
    {
        msg: Uncaught TypeError: Cannot set property 'value' of null,
        line: 1
    }", source: https://mail.metropolitan.ac.rs/m/zmain (10)
    [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'value' of null", source: https://mail.metropolitan.ac.rs/m/zmain (1)

所以,如果url从https://mail.metropolitan.ac.rs/ 至https://mail.metropolitan.ac.rs/m/zmain,表示登录成功。但是正如你所看到的,代码运行了两次,第一次它说它没有连接,但是第二次它说它连接了。

bxfogqkk

bxfogqkk1#

如果onpagestarted方法启动onpagefinished后url正常,但如果onpagestarted启动后url被重定向,则应覆盖OnPageLoading,然后再覆盖onpagefinished。你应该检查加载的url是否被重定向

private boolean isRedirected;

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {   

  if (!isRedirected) {      
    //Do something you want when starts loading
  }

  isRedirected = false;
}

如果url被重定向,回调将启动此函数

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

  view.loadUrl(url);
  isRedirected = true;
  return true;
}

在onpagefinished中执行某些操作之前,请检查回调是否已进入shouldoverrideurlloading方法

@Override
public void onPageFinished(WebView view, String url) {

  if (!isRedirected) {
    //Do something you want when finished loading 
  }
}

相关问题