Home > other >  Webview Java Android Studio didnt open one page
Webview Java Android Studio didnt open one page

Time:04-25

The registration page does not open in the application, all other pages work. This page is powered by a mobile website built for web browsing. This is my first application.

I tried a dozen different ways from those suggested in the answers to other questions. Everywhere something does not fit. How should one go about solving such issues?

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));
        }
    }
}

}

this works in kotlin app but i can't make image upload work there

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)
    }
}

CodePudding user response:

You are using onRECEIVE_ERROR method instead of onRECEIVE_SSL_ERROR method. Add the below code on top of webview.loadurl("some url");. Java sample code:

  wview.setWebViewClient(new WebViewClient(){

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

Also add all the webview.settings method you have added in kotlin

  • Related