Android webview无法加载某些页面

时间:2016-09-11 18:36:31

标签: android webview chromium

我有一个简单的webview来通过网络显示网页。它可以很好地加载大多数页面,但每当我尝试加载http://www.nytimes.com时,它都会显示为空白。此时的logcat是:

09-11 11:26:18.428 10567-10630/com.project.test.webviewtest W/cr_media: Requires BLUETOOTH permission
09-11 11:26:18.454 10567-10567/com.project.test.webviewtest W/cr_AwContents: onDetachedFromWindow called when already detached. Ignoring
09-11 11:26:18.470 10567-10567/com.project.test.webviewtest I/cr_Ime: ImeThread is not enabled.
09-11 11:26:18.486 10567-10650/com.project.test.webviewtest D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
09-11 11:26:18.512 10567-10657/com.project.test.webviewtest E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
09-11 11:26:18.530 10567-10650/com.project.test.webviewtest I/OpenGLRenderer: Initialized EGL, version 1.4
09-11 11:26:18.538 10567-10657/com.project.test.webviewtest W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
09-11 11:26:18.557 10567-10657/com.project.test.webviewtest I/VideoCapabilities: Unsupported profile 4 for video/mp4v-es
09-11 11:26:18.926 10567-10567/com.project.test.webviewtest W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 10567
09-11 11:26:18.946 10567-10567/com.project.test.webviewtest W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 10567
09-11 11:26:18.963 10567-10567/com.project.test.webviewtest W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 10567
09-11 11:26:19.313 10567-10567/com.project.test.webviewtest W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 10567
09-11 11:26:19.315 10567-10567/com.project.test.webviewtest W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 10567
09-11 11:26:19.316 10567-10567/com.project.test.webviewtest W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 10567
09-11 11:26:19.339 10567-10567/com.project.test.webviewtest W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 10567
09-11 11:26:19.438 10567-10628/com.project.test.webviewtest W/chromium: [WARNING:spdy_session.cc(2462)] Received HEADERS for invalid stream 3

我的MainActivity

public class MainActivity extends AppCompatActivity {
private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.setWebViewClient(new TestWebViewClient());
    mWebView.loadUrl("http://www.nytimes.com");
}
}

我的TestWebViewClient

public class TestWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    return true;
}

@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    Log.i("WEB_VIEW_TEST", "error code:" + errorCode);
    super.onReceivedError(view, errorCode, description, failingUrl);
}
}

我在清单中添加了INTERNET权限。到目前为止,我试过的任何其他网页都很好。只是这一个显示为空白。我不确定logcat是否显示实际错误,因为EGL_BAD_DISPLAYCannot call determinedVisibility()错误等常常会出现在成功加载中。我通常不会看到最后一行[WARNING:spdy_session.cc(2462)] Received HEADERS for invalid stream 3所以这可能引起关注?我在onReceivedError的{​​{1}}中未收到任何错误。

有时候(很少),当我在全新安装后启动应用程序时,“纽约时报”页面标题会在视图显示为空白之前显示一秒钟,这可能表明这是一个渲染问题而不是问题网络视图。

我在运行Android M.的物理设备上运行它。

更新:我尝试在TestwebViewClient打印网址,最初转到正确的重定向网址:shouldOverrideUrlLoading,然后切换到http://mobile.nytimes.com/?referer=。我不知道该怎么做以及为什么要切换到这个。重定向本身不应该是一个问题,因为data:text/html在重定向到youtube.com之后正确加载

2 个答案:

答案 0 :(得分:0)

所以我想出了问题,但仍然不知道原因。如果我在TestWebViewClient.中注释<h2>中国China</h2> ,则会打开该页面我不确定为什么这只是这个页面的问题,但它适用于其他页面。

答案 1 :(得分:0)

尝试将http://www.nytimes.com更改为https://www.nytimes.com

不允许加载不安全的URL