Android-WebView路由登入192.168.1.1

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   os   sp   for   on   

由於項目需求製做路由器的登入,有的路由可以直接開啟網頁登入,有的是開啟的登入窗

我們的路由是開啟的登入視窗,我在自己寫的時候用WebView打不開登入窗

在網上搜尋的答案是WebView.setHttpAuthUsernamePassword(host, realm, username, password)WebView增加WebChromeClient重寫onJsAlert發現以上辦法都不能解決問題 用瀏覽器開啟的時候會彈出登入框,當我用Android WebView登入的時候提示登入失敗打不開登入框,一開始我以為是js產生的彈出框實驗給WebView setWebChromeClient 重寫 WebChromeClient 中的
public boolean onJsAlert(WebView view, String url, String message, JsResult result) public boolean onJsConfirm(WebView view, String url, String message, JsResult result)public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result)public boolean onJsBeforeUnload(WebView view, String url, String message, JsResult result)public boolean onJsTimeout()
發現以上方法沒有一個執行到瞭然後我在對比Firefox,IE,Google Chrome感覺是瀏覽器自己做的瀏覽器開啟的時候會提示輸入使用者名稱和密碼 發現WebViewClient中有一個方法 onReceivedHttpAuthRequest然後查看源碼
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {         handler.cancel();}
發現是直接調用的 handler.cancel();給WebView增加WebViewClient 重寫 onReceivedHttpAuthRequest 將此方法置空發現登入的時候會停止在一個空白頁面,等待處理,並沒有像開始直接開啟的時候提示登入失敗 然後查看onReceivedHttpAuthRequest中的參數有一個HttpAuthHandler 源碼預設是調用的cancel() 發現還有一個方法 proceed(String username, String password) 然後在重寫 onReceivedHttpAuthRequest時
public void onReceivedHttpAuthRequest(WebView view,  HttpAuthHandler handler, String host, String realm) {         handler.proceed("admin", "admin");} // PS: admin 是我路由器預設的帳號和密碼

然後就登入進去了我的路由器,問題到這裡已經解決

onReceivedHttpAuthRequest這個方法也是在主線程中的,可以在這裡製做自己的登入視窗

Android-WebView路由登入192.168.1.1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.