2015-04-18 64 views
2

我現在面臨的問題加載URL到的WebView總是顯示的空白頁,我看不到任何錯誤,我logcat中。它在5.0 [Lollipop]設備以下工作正常,僅在棒棒糖設備中出現錯誤。我嘗試了很多存在於StackOverflow中的解決方案,但都沒有工作。 下面是我最終的代碼。 網站我試圖負載:https://netbanking.hdfcbank.com/netbanking/ 任何幫助將感謝理解了很多。的WebView顯示空白頁 - 棒棒堂

import android.annotation.TargetApi; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.ProgressDialog; 
import android.content.DialogInterface; 
import android.net.http.SslError; 
import android.os.Build; 
import android.os.Bundle; 
import android.util.Log; 
import android.webkit.CookieManager; 
import android.webkit.SslErrorHandler; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

/** 
* Created by shetty on 4/18/2015. 
*/ 
public class WebPageTest extends Activity { 

    WebView webView; 

    /** Called when the activity is first created. */ 
    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_web_page); 
     String url = getIntent().getStringExtra("url"); 
     webView = (WebView) findViewById(R.id.webview); 
     WebSettings settings = webView.getSettings(); 
     settings.setJavaScriptEnabled(true); 

     // webView.clearSslPreferences(); 
     settings.setDomStorageEnabled(true); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); 
      webView.enableSlowWholeDocumentDraw(); 
     } 
     CookieManager cookieManager = CookieManager.getInstance(); 
     cookieManager.setAcceptThirdPartyCookies(webView,true); 
     webView.setWebViewClient(new SSLTolerentWebViewClient() { 
      @Override 
      public void onReceivedSslError(final WebView view, final SslErrorHandler handler, SslError error) { 
       Log.d("CHECK", "onReceivedSslError"); 
       AlertDialog.Builder builder = new AlertDialog.Builder(WebPageTest.this); 
       AlertDialog alertDialog = builder.create(); 
       String message = "Certificate error."; 
       switch (error.getPrimaryError()) { 
        case SslError.SSL_UNTRUSTED: 
         message = "The certificate authority is not trusted."; 
         break; 
        case SslError.SSL_EXPIRED: 
         message = "The certificate has expired."; 
         break; 
        case SslError.SSL_IDMISMATCH: 
         message = "The certificate Hostname mismatch."; 
         break; 
        case SslError.SSL_NOTYETVALID: 
         message = "The certificate is not yet valid."; 
         break; 
       } 
       message += " Do you want to continue anyway?"; 
       alertDialog.setTitle("SSL Certificate Error"); 
       alertDialog.setMessage(message); 
       alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         Log.d("CHECK", "Button ok pressed"); 
         // Ignore SSL certificate errors 
         handler.proceed(); 
        } 
       }); 
       alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         Log.d("CHECK", "Button cancel pressed"); 
         handler.cancel(); 
        } 
       }); 
       alertDialog.show(); 
      } 
     }); 
     webView.loadUrl(url); 
    } 
} 

回答

7

管理摸不着頭腦。關於你的網站和我一直在努力的一件事是,他們試圖佔用全屏高度。我的網頁流量(我想你也一樣)有layout_height =「WRAP_CONTENT」,雖然它有,因爲相對佈局內定位適當的高度,針對SDK時21 WebView中被報告高度爲0到頁面產生的所有元素崩潰0高度以及。您只需將webview的layout_height更改爲match_parent即可。

臨提示爲開發者提供的WebView呈現問題掙扎。使用webview遠程調試https://developer.chrome.com/devtools/docs/remote-debugging 在處理類似問題時,可以節省大量時間。

+0

感謝哥們,無論如何解決了這個問題,使用一個示例GutHub項目。但這是我的問題的確切解決方案。感謝您的幫助 ! – user3458008

0

這裏是一個列表的更改webview的lolipop,也許這有助於。

的是Android 5.0更改爲您的應用程序的默認行爲。

如果您的應用程序的目標API級別21或更高版本: 系統塊混合內容和第三方的cookies默認。要允許混合內容和第三方cookie,分別使用setMixedContentMode()和setAcceptThirdPartyCookies()方法。 系統現在智能地選擇HTML文檔的一部分進行繪製。這種新的默認行爲有助於減少內存佔用並提高性能。如果要一次渲染整個文檔,請通過調用enableSlowWholeDocumentDraw()來禁用此優化。 如果您的應用程序的目標API級別低於21:系統允許混合內容和第三方Cookie,並且始終呈現整個文檔。

來源:http://developer.android.com/about/versions/android-5.0-changes.html#BehaviorWebView

+0

感謝您的回覆。我試了三個並更新了我的問題。但仍面臨同樣的問題。 – user3458008