webview没有加载特定网址

时间:2018-04-16 09:56:18

标签: android url webview

这是我的URL

我正在尝试在webview中加载此网址,但它没有加载。这个URL完全可以在网络浏览器上运行。

webview只是不加载此URL

这是我的代码

webView.setWebChromeClient(new MyWebChromeClient(this));
    webView.setWebViewClient(new WebViewClient() {

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

            if (Utils.isSameDomain(postUrl, url)) {
                Intent intent = new Intent(MainActivity.this, MainActivity.class);
                intent.putExtra("postUrl", url);
                startActivity(intent);
            } else {
                openInAppBrowser(url);
            }
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressBar.setVisibility(View.GONE);
        }
    });
    webView.clearCache(true);
    webView.clearHistory();
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.setHorizontalScrollBarEnabled(false);
    webView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getPointerCount() > 1) {
                return true;
            }
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    m_downX = event.getX();
                }
                break;

                case MotionEvent.ACTION_MOVE:
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP: {
                    event.setLocation(m_downX, event.getY());
                }
                break;
            }
            return false;
        }
    });

任何帮助都将受到高度赞赏。

提前致谢

3 个答案:

答案 0 :(得分:1)

使用以下代码加载网址

public class TestingActivity extends AppCompatActivity
 {

    private WebView mWebview;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mWebview = new WebView(this);

        mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript

        final Activity activity = this;

        mWebview.setWebViewClient(new WebViewClient() {
            @SuppressWarnings("deprecation")
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }

            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
                // Redirect to deprecated method, so you can use it in all SDK versions
                onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
            }
        });

        mWebview.loadUrl("http://api.tixbag.com/test.php");
        setContentView(mWebview);

    }

}

答案 1 :(得分:1)

我只需在加载网址之前启用javascript即可在我的网络视图中加载http://api.tixbag.com/test.php

示例代码:

mWebView.getSettings().setJavaScriptEnabled(true);

mWebView.loadUrl("http://api.tixbag.com/test.php");

答案 2 :(得分:1)

试试这个

public class MainActivity extends AppCompatActivity {

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

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

        webView.getSettings().setJavaScriptEnabled(true);

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

        webView.setWebViewClient(new WebViewController());

        webView.loadUrl("http://api.tixbag.com/test.php");


    }
    public class WebViewController extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

<强>输出

enter image description here