Webview Java Android Studio 没有打开一个页面

Webview Java Android Studio didnt open one page

注册页面无法在应用程序中打开,所有其他页面都可以。此页面由专为网络浏览而构建的移动网站提供支持。这是我的第一个应用程序。

我尝试了十几种与其他问题的答案中建议的不同的方法。到处都有东西不适合。应该如何解决这些问题?

import static android.webkit.WebChromeClient.FileChooserParams.parseResult;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.SslErrorHandler;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    private WebView webView;
    private UploadSupport uploadSupport;
    private ValueCallback<Uri[]> mUMA;
    private String mainPage = "...";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    uploadSupport = new UploadSupport(MainActivity.this);

    webView = findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setAllowContentAccess(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().getLoadWithOverviewMode();

    webView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(
                WebView view,
                WebResourceRequest request
            ){

            return super.shouldOverrideUrlLoading(view, request);
        }

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

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
        }
    });

    webView.setWebChromeClient(new WebChromeClient(){
        @Override
        public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
            uploadSupport.showFileChooser(filePathCallback);
            mUMA = filePathCallback;
            return true;
        }
    });
    webView.loadUrl(mainPage);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode==UploadSupport.uploadCode){
        if (grantResults.length>0&&grantResults[0]== PackageManager.PERMISSION_GRANTED){
            uploadSupport.showFileChooser(mUMA);
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode==UploadSupport.FCR){
        if (uploadSupport.mUMA!=null){
            uploadSupport.mUMA.onReceiveValue(parseResult(resultCode, data));
        }
    }
}
}

这在 kotlin 应用程序中有效,但我无法在那里进行图片上传

import android.annotation.SuppressLint
import android.net.Uri
import android.net.http.SslError
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.webkit.SslErrorHandler
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient

val mainPage = "...."
var urls = ArrayList<Uri>()

@Suppress("DEPRECATED_IDENTITY_EQUALS")
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val vw : WebView = findViewById(R.id.my_web_view)
        vw.settings.javaScriptEnabled = true
        vw.settings.domStorageEnabled = true
        vw.settings.allowFileAccess = true
        vw.settings.allowContentAccess = true
        vw.settings.loadWithOverviewMode = true

        vw.webViewClient = object : WebViewClient() {
            @SuppressLint("WebViewClientOnReceivedSslError")
            override
            fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
                handler?.proceed()
            }
        }

        object : WebViewClient() {
            override fun shouldOverrideUrlLoading(
                view: WebView?,
                request: WebResourceRequest?
            ): Boolean {

                request?.let { urls.add(it.url) }
                if (urls.count() > 10)
                    urls.removeAt(0)

                return super.shouldOverrideUrlLoading(view, request)
            }
        }.also { vw.webViewClient = it }

        vw.loadUrl(mainPage)
    }
}

正如 Peer Mohamed 所说,

我加了

WebSettings webSettings = webView.getSettings();
webSettings.setDomStorageEnabled (true);
webSettings.setLoadWithOverviewMode (true);

webView.setWebViewClient(new WebViewClient(){
    
   @SuppressLint("WebViewClientOnReceivedSslError")
   @Override
   public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
       handler.proceed();
   }        
    
});

超过

webView.loadUrl(mainPage);

成功了

您正在使用 onRECEIVE_ERROR 方法而不是 onRECEIVE_SSL_ERROR 方法。在 webview.loadurl("some url"); 之上添加以下代码。 Java 示例代码:

  wview.setWebViewClient(new WebViewClient(){

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//                super.onReceivedSslError(view, handler, error);
                handler.proceed();
            }
        });

同时添加您在 kotlin 中添加的所有 webview.settings 方法