Java android的webview显示空白屏幕

Java android的webview显示空白屏幕,java,android,Java,Android,我正在尝试在android中为站点localbestprice.com创建一个网络视图 我不熟悉这个程序。我尝试了很多方法,但它只显示空白屏幕。已尝试重写true和false的方法。问题仍未解决 我正在使用下面的代码 清单文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.localb

我正在尝试在android中为站点
localbestprice.com
创建一个网络视图

我不熟悉这个程序。我尝试了很多方法,但它只显示空白屏幕。已尝试重写true和false的方法。问题仍未解决

我正在使用下面的代码

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.localbestprice.localbest">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
    android:allowBackup="true"
   android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context="com.localbestprice.localbest.MainActivity">


       <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

</android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity {

private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webView = (WebView) findViewById(R.id.webView);

    webView.getSettings().setJavaScriptEnabled(true);

    webView.loadUrl("https://www.localbestprice.com");

}

public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        view.loadUrl(url);
        return true;

    }

}

@Override
// This method is used to detect back button
public void onBackPressed() {
    if(webView.canGoBack()) {
        webView.goBack();
    } else {
        // Let the system handle the back button
        super.onBackPressed();
    }
}
}
}
Java代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.localbestprice.localbest">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
    android:allowBackup="true"
   android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context="com.localbestprice.localbest.MainActivity">


       <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

</android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity {

private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webView = (WebView) findViewById(R.id.webView);

    webView.getSettings().setJavaScriptEnabled(true);

    webView.loadUrl("https://www.localbestprice.com");

}

public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        view.loadUrl(url);
        return true;

    }

}

@Override
// This method is used to detect back button
public void onBackPressed() {
    if(webView.canGoBack()) {
        webView.goBack();
    } else {
        // Let the system handle the back button
        super.onBackPressed();
    }
}
}
}
之后使用此代码

webView.getSettings().setJavaScriptEnabled(true)

并删除此
myWebClient类

之后使用此代码

webView.getSettings().setJavaScriptEnabled(true)


并删除此
myWebClient类

尝试为您的
webView
提供约束。如下所示更改xml

<android.support.constraint.ConstraintLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context="com.localbestprice.localbest.MainActivity">

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

尝试为您的
网络视图提供约束。如下所示更改xml

<android.support.constraint.ConstraintLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context="com.localbestprice.localbest.MainActivity">

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

当您使用https url时,需要将以下内容设置为您的Web视图

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);

webView.setWebViewClient(new WebViewClient() {

    private int webViewPreviousState;
    private final int PAGE_STARTED = 0x1;
    private final int PAGE_REDIRECTED = 0x2;

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        // Get cert from SslError
        SslCertificate sslCertificate = error.getCertificate();
        Certificate cert = getX509Certificate(sslCertificate);
        if (cert != null && certificate != null) {
            try {
                // Reference: https://developer.android.com/reference/java/security/cert/Certificate.html#verify(java.security.PublicKey)
                cert.verify(certificate.getPublicKey()); // Verify here...
                handler.proceed();
            } catch (CertificateException | NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException | SignatureException e) {
                super.onReceivedSslError(view, handler, error);
                e.printStackTrace();
            }
        } else {
            super.onReceivedSslError(view, handler, error);
        }
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
        webViewPreviousState = PAGE_REDIRECTED;
        wv_main.loadUrl(urlNewString);
        return true;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        webViewPreviousState = PAGE_STARTED;
        /*if (dialog == null || !dialog.isShowing())
        dialog = ProgressDialog.show(MainActivity.this, "", "Loading", true, true);*/
    }

    @Override
    public void onPageFinished(WebView view, String url) {

        if (webViewPreviousState == PAGE_STARTED) {
            //dialog.dismiss();
            swipeToRefresh.setRefreshing(false);
        }
    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request,
                                WebResourceError error) {
        super.onReceivedError(view, request, error);
        Toast.makeText(MainActivity.this, "Error Loading Screen.", Toast.LENGTH_SHORT).show();
    }
});
这是获取证书的方法的代码

private Certificate getX509Certificate(SslCertificate sslCertificate) {
        Bundle bundle = SslCertificate.saveState(sslCertificate);
        byte[] bytes = bundle.getByteArray("x509-certificate");
        if (bytes == null) {
            return null;
        } else {
            try {
                CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
                return certFactory.generateCertificate(new ByteArrayInputStream(bytes));
            } catch (CertificateException e) {
                return null;
            }
        }
    }

当您使用https url时,您需要在webview中设置以下内容

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);

webView.setWebViewClient(new WebViewClient() {

    private int webViewPreviousState;
    private final int PAGE_STARTED = 0x1;
    private final int PAGE_REDIRECTED = 0x2;

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        // Get cert from SslError
        SslCertificate sslCertificate = error.getCertificate();
        Certificate cert = getX509Certificate(sslCertificate);
        if (cert != null && certificate != null) {
            try {
                // Reference: https://developer.android.com/reference/java/security/cert/Certificate.html#verify(java.security.PublicKey)
                cert.verify(certificate.getPublicKey()); // Verify here...
                handler.proceed();
            } catch (CertificateException | NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException | SignatureException e) {
                super.onReceivedSslError(view, handler, error);
                e.printStackTrace();
            }
        } else {
            super.onReceivedSslError(view, handler, error);
        }
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
        webViewPreviousState = PAGE_REDIRECTED;
        wv_main.loadUrl(urlNewString);
        return true;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        webViewPreviousState = PAGE_STARTED;
        /*if (dialog == null || !dialog.isShowing())
        dialog = ProgressDialog.show(MainActivity.this, "", "Loading", true, true);*/
    }

    @Override
    public void onPageFinished(WebView view, String url) {

        if (webViewPreviousState == PAGE_STARTED) {
            //dialog.dismiss();
            swipeToRefresh.setRefreshing(false);
        }
    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request,
                                WebResourceError error) {
        super.onReceivedError(view, request, error);
        Toast.makeText(MainActivity.this, "Error Loading Screen.", Toast.LENGTH_SHORT).show();
    }
});
这是获取证书的方法的代码

private Certificate getX509Certificate(SslCertificate sslCertificate) {
        Bundle bundle = SslCertificate.saveState(sslCertificate);
        byte[] bytes = bundle.getByteArray("x509-certificate");
        if (bytes == null) {
            return null;
        } else {
            try {
                CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
                return certFactory.generateCertificate(new ByteArrayInputStream(bytes));
            } catch (CertificateException e) {
                return null;
            }
        }
    }