Android webview无法加载某些页面

问题描述 投票:0回答:5

我有一个简单的网络视图来通过网络显示网页。它可以很好地加载大多数页面,但每当我尝试加载

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_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

后可以正确加载
android webview chromium
5个回答
1
投票

所以我找到了问题所在,但仍然不知道原因。如果我在

shouldOverrideUrlLoading
中注释掉
TestWebViewClient.
,该页面就会打开。我不确定为什么这只是此页面的问题,但它适用于其他页面。


0
投票

尝试将

http://www.nytimes.com
更改为
https://www.nytimes.com

不允许加载不安全的URL


0
投票

我遇到了这个问题并删除了

view.loadUrl(url);

来自shouldOverrideUrlLoading解决了我的问题。 也许对你有帮助。


0
投票

你可以试试这个网站(来自官方android.com) https://developer.android.com/guide/webapps/webview


0
投票

@Black 说,尝试 https 而不是 http 大多数情况下,这可能是错误的原因。

此外,当我在边缘浏览器上输入 http://www.nytimes.com 时,它会将其更正为 https://www.nytimes.com

© www.soinside.com 2019 - 2024. All rights reserved.