Android webview无法加载某些页面

doo*_*guy 5 android webview chromium

我有一个简单的网络视图来通过网络显示网页。它可以很好地加载大多数页面,但每当我尝试加载时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
Run Code Online (Sandbox Code Playgroud)

我的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");
}
}
Run Code Online (Sandbox Code Playgroud)

我的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);
}
}
Run Code Online (Sandbox Code Playgroud)

我已经在清单中添加了 INTERNET 权限。到目前为止,我尝试过的任何其他网页都可以正常加载。仅此一项显示为空白。我不确定 logcat 是否显示实际错误,因为诸如 和 之类的EGL_BAD_DISPLAY错误Cannot call determinedVisibility()也经常出现在成功的加载中。我通常不会看到最后一行[WARNING:spdy_session.cc(2462)] Received HEADERS for invalid stream 3,所以也许这是一个令人担忧的原因?我没有看到 中收到任何onReceivedError错误TestwebViewClient

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

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

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

doo*_*guy 1

所以我弄清楚了这个问题,但仍然不知道原因。如果我注释掉该页面shouldOverrideUrlLoadingTestWebViewClient.我不确定为什么这只是此页面的问题,但它适用于其他页面。