webView white screen

Asked 2 years ago, Updated 2 years ago, 27 views

public class MainActivity extends AppCompatActivity {

    private WebView webView;
    private TextView txt_address;
    private Handler handler;

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

        txt_address = findViewById(R.id.txt_address);

        // WebView Initialization
        init_webView();

        // JavaScript event response via handler
        handler = new Handler();
    }

    public void init_webView() {

        // WebView Settings
        webView = (WebView) findViewById(R.id.webView_address);

        webView.setWebViewClient(new WebViewClient() {

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

        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

        webView.setWebViewClient(new SslWebViewConnect());
        // Allow JavaScript
        webView.getSettings().setJavaScriptEnabled(true);

        // Allow window.open in JavaScript
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        // Assign a class that defines a function to correspond to JavaScript events
        webView.addJavascriptInterface(new AndroidBridge(), "TestApp");

        // Set web client to Chrome
        webView.setWebChromeClient(new WebChromeClient());

        webView.getSettings().setDomStorageEnabled(true);

    }

    // webview url load.php file address
    // // webView.loadUrl("http://naver.com");
    // This is the bottom
    // I put the php address.

    private class AndroidBridge {
        @JavascriptInterface
        public void setAddress(final String arg1) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    txt_address.setText(String.format(arg1));

                    // Cannot be reused without initializing WebView
                    init_webView();
                }
            });
        }
    }

    public class SslWebViewConnect extends WebViewClient {

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.process(); // proceed even if SSL error occurs!
        }

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;// application handles url directly
        }
    }
}

Hello, everyone I'm going to use the following address API to search for addresses It comes up well on Android Studio devices When I debug it on my phone, the web view only comes out on the white screen.

The mobile phone version is 8.0, 6.0, and the device version is 9.0, 8.0 Cell phones were both white screens and devices were both working.

Below is a php file that hands over php data.

<?php
header("Content-Type: text/html; charset=UTF-8");
?>

<script src="https://t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js"></script>
<script>
new daum.Postcode({
    oncomplete: function(data) {
        if(data.userSelectedType=="R"){
            // userSelectedType : The type of address selected by the user in the search results
            // // return type : R - roadAddress, J : jibunAddress
            // TestApp is the name registered on Android
            window.TestApp.setAddress(data.roadAddress);
        }
        else{
            window.TestApp.setAddress(data.jibunAddress);
        }        
    }
}).open();
</script>

android java

2022-09-22 18:15

1 Answers

 <?php

header("Content-Type: text/html; charset=UTF-8");

?>



<script src="https://t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js"></script>

<script>

new daum.Postcode({

    oncomplete: function(data) {

        if(data.userSelectedType=="R"){

            // userSelectedType : The type of address selected by the user in the search results

            // // return type : R - roadAddress, J : jibunAddress

            // TestApp is the name registered on Android

            window.TestApp.setAddress(data.roadAddress);

        }

        else{

            window.TestApp.setAddress(data.jibunAddress);

        }        

    }

}).open();

</script>

This is the php code I'm using now


2022-09-22 18:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.