繁体   English   中英

URL未加载到WebView中但加载到了Android浏览器中?

[英]URL not loading in webview but loaded in browser in android?

某些类型的Urls无法在Webview的应用程序中加载,但可以在设备浏览器中加载。 以下是在我的代码中不起作用的示例网址-“ http://apps.takeyourapp.com/testApp/staging/index.html

package com.example.webviewdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity {

    WebView webViewPlaceholder;
    String URL = "http://apps.takeyourapp.com/testApp/staging/index.html";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webViewPlaceholder = (WebView) findViewById(R.id.webholder);
        webViewPlaceholder.getSettings().setJavaScriptEnabled(true);
        webViewPlaceholder
                .setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webViewPlaceholder.setScrollbarFadingEnabled(true);
        webViewPlaceholder.getSettings().setLoadsImagesAutomatically(true);
        webViewPlaceholder.getSettings().setUseWideViewPort(true);
        webViewPlaceholder.loadUrl(URL);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

创建WebViewClient并加载如下url

 public class myWebClient extends WebViewClient {
    @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                view.loadUrl(url);
                return true;
            }
}

setWebViewClient一样:

    webView.setWebViewClient(new myWebClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://apps.takeyourapp.com/testApp/staging/index.html");

并将INTERNET权限添加到manifest.xml

<uses-permission android:name="android.permission.INTERNET" />

试试这个解决方案。

public class Main extends Activity {

    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() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }
        });

        mWebview .loadUrl("http://apps.takeyourapp.com/testApp/staging/index.html");
        setContentView(mWebview );

    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM